hi guys can u help me with this code, the error in wamp is [TABLE="class: xdebug-error xe-notice"] [TR] [TH="bgcolor: #f57900, colspan: 5, align: left"]Notice: Undefined index: user in C:\wamp\www\index.php on line 53[/TH] [/TR] [TR] [TH="bgcolor: #e9b96e, colspan: 5, align: left"]Call Stack[/TH] [/TR] [TR] [TH="bgcolor: #eeeeec, align: center"]#[/TH] [TH="bgcolor: #eeeeec, align: left"]Time[/TH] [TH="bgcolor: #eeeeec, align: left"]Memory[/TH] [TH="bgcolor: #eeeeec, align: left"]Function[/TH] [TH="bgcolor: #eeeeec, align: left"]Location[/TH] [/TR] [TR] [TD="bgcolor: #eeeeec, align: center"]1[/TD] [TD="bgcolor: #eeeeec, align: center"]0.0007[/TD] [TD="bgcolor: #eeeeec, align: right"]678552[/TD] [TD="bgcolor: #eeeeec"]{main}( )[/TD] [TD="bgcolor: #eeeeec"]..\index.php:0[/TD] [/TR] [/TABLE] code is <?php $con = mysql_connect("localhost","aneeb",""); if (!$con) { die('Could not connect: ' . mysql_error()); } $selected = mysql_select_db("b_tips",$con) or die("Could Not Select Db"); $mysql_post="INSERT INTO tips (User, Title, Tip) VALUES ('$_POST[user]','$_POST[title]','$_POST[tip]')"; if (!mysql_query($mysql_post,$con)) { die ('Error: ' . mysql_error()); } echo "1 Record Added"; mysql_close($con); ?>
Does the form have a field with the name "user"? Also you should also use mysql_real_escape_string() to avoid SQL injection attacks or better still use parametrized queries e.g.: $db = new PDO('mysql:dbname=b_tips;host=localhost', 'aneeb', ''); $stmt = $db->prepare('INSERT INTO tips (User, Title, Tip) VALUES (:user, :title, :tip)'); $result = $stmt->execute(array( ':user' => $_POST['user'], ':title' => $_POST['title'], ':tip' => $_POST['tip'] )); if(!$result) die('Error: ' . $sth->errorInfo()[2]); echo "1 Record Added"; PHP:
thanks for the tip and help, i have a field name user, stille facing the problem, form is working proprely .. but when i refresh the page on localhost, it automatically adds a blank data entry
Try adding a check to make sure that the form has been submitted: // make sure the form has been submitted if($_POST) { // Form has been submitted so insert it $db = new PDO('mysql:dbname=b_tips;host=localhost', 'aneeb', ''); $stmt = $db->prepare('INSERT INTO tips (User, Title, Tip) VALUES (:user, :title, :tip)'); $result = $stmt->execute(array( ':user' => $_POST['user'], ':title' => $_POST['title'], ':tip' => $_POST['tip'] )); if(!$result) die('Error: ' . $sth->errorInfo()[2]); echo "1 Record Added"; } PHP:
this code also have error die('Error: ' . $sth->errorInfo () [2] ); but when i remove [2] its works but Still by refreshing the page it repeats the last entry in database
$mysql_post="INSERT INTO tips (User, Title, Tip) VALUES ('$_POST[\'user\']','$_POST[\'title\']','$_POST[\'tip\']')";
it's saying you don;t have a index user and why it's coming $_POST['user']; not $_POST[user]; check it and tell here..