Writing to MySQL Database please help

lw2472

New Member
Messages
1
Reaction score
0
Points
0
Can someone tell me why this isnt working? i am receiving a database not updated error, it should be showing the database updated, id, first name, last name, phone.

please thank you in advance..

// <?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];

mysql_connect ("localhost", "lddenter_xxx", "xxx") or die (' Error: ' .mysql_error());
mysql_select_db ("lddenter_test");

$query="INSERT INTO commentTable (ID, firstname, lastname, phone)
VALUES ('NULL','".$firstname."','".$lastname."','".phone."')";

mysql_query($query) or die ('Error Updating Database');

$result = mysql_query ($query);

echo('Database Updated With: ' . $firstname . ' ' .$lastname . ' ' . $phone);

?> //

the html


// <body>
<form id="form1" name="form1" method="post" action="test.php">
<p>First Name:<br >
<input name="firstname" type="text" id="firstname" />
<br >
<br > Last Name:<br >
<label>
<input type="text" name="lastname" id="lastname" >
</label>
<br ><br >
Phone Number:<br >
<label>
<input type="text" name="phone" id="phone">
</label>
<br ><br >
<label>
<input type="submit" name="Submit" id="Submit" value="Submit Customer Information" >
</label>
<br >
</form>
</body> //
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
  • When asking for help, post the behavior you expect and the behavior you get, which includes error messages.
  • When posting any sort of code, use
    Code:
    , [PHP] or [HTML] tags (as appropriate) to delineate the code.
    [*]Your code is vulnerable to [url=http://unixwiz.net/techtips/sql-injection.html]SQL injection[/url]. See the next point.
    [*]The mysql driver is terribly out of date. Use [URL="http://php.net/PDO"]PDO[/URL] and [URL="http://www.php.net/pdo.prepared-statements"]prepared statements[/URL], which are invulnerable to SQL injection.
    [/list]
 

guimar

New Member
Messages
13
Reaction score
0
Points
0
Can someone tell me why this isnt working? i am receiving a database not updated error, it should be showing the database updated, id, first name, last name, phone.

please thank you in advance..

// <?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];

mysql_connect ("localhost", "lddenter_xxx", "xxx") or die (' Error: ' .mysql_error());
mysql_select_db ("lddenter_test");

$query="INSERT INTO commentTable (ID, firstname, lastname, phone)
VALUES ('NULL','".$firstname."','".$lastname."','".phone."')";

mysql_query($query) or die ('Error Updating Database');

$result = mysql_query ($query);

echo('Database Updated With: ' . $firstname . ' ' .$lastname . ' ' . $phone);

?> //

the html


// <body>
<form id="form1" name="form1" method="post" action="test.php">
<p>First Name:<br >
<input name="firstname" type="text" id="firstname" />
<br >
<br > Last Name:<br >
<label>
<input type="text" name="lastname" id="lastname" >
</label>
<br ><br >
Phone Number:<br >
<label>
<input type="text" name="phone" id="phone">
</label>
<br ><br >
<label>
<input type="submit" name="Submit" id="Submit" value="Submit Customer Information" >
</label>
<br >
</form>
</body> //


you are running the sql iinsert twice, if any of the columns have a unique attribute the second insert fails.

mysql_query($query) or die ('Error Updating Database');

$result = mysql_query ($query);
should be
$result =mysql_query($query) or die ('Error Updating Database');
or even more helpful
$result =mysql_query($query) or die('Error Updating Database:'.mysql_error());
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
$query="INSERT INTO commentTable (ID, firstname, lastname, phone)
VALUES ('NULL','".$firstname."','".$lastname."','".phone. "')";


Don't you mean $phone
 
Top