okrobie Posted January 11, 2010 Share Posted January 11, 2010 This should be an easy script but I can't get it to run. Can someone please help me? <html><head><title>Adding a User</title></head> <body> <?php $self = $_SERVER['PHP_SELF']; $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $user_email = $_POST['useremail']; if( (!$first_name) or (!$last_name) or (!$user_email) ) { $form ="Please enter all new user details..."; $form.="<form action=\"$self\""; $form.=" method=\"post\">First Name: "; $form.="<input type=\"text\" name=\"firstname\""; $form.=" value=\"$first_name\"><br>Last Name: "; $form.="<input type=\"text\" name=\"lastname\""; $form.=" value=\"$last_name\"><br>Email: "; $form.="<input type=\"text\" name=\"useremail\""; $form.=" value=\"$user_email\"><br>"; $form.="<input type=\"submit\" value=\"Submit\">"; $form.="</form>"; echo($form); } else { #connect to MySQL $conn = @mysql_connect("localhost","xxxx","xxxx") or die("Could not connect to MySQL"); #select a database $db = @mysql_select_db("xxxx",$conn) or die("Could not select database"); #create the SQL query $sql = "insert into tablename (firstname, lastname, useremail) values (\"$first_name\",'\"$last_name\",\"$user_email\" )"; #execute the query $result = @mysql_query($sql,$conn) or die("Could not execute query"); if($result) { echo("New user $user_email added"); } } ?> </body></html> Link to comment https://forums.phpfreaks.com/topic/188005-add-user-script-could-not-execute-query/ Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 Instead of simply displaying a useless message, try echo'ing mysql_error. Link to comment https://forums.phpfreaks.com/topic/188005-add-user-script-could-not-execute-query/#findComment-992558 Share on other sites More sharing options...
okrobie Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks for your help. I tried following the example given but I keep getting unexpected T_ECHO I don't know the proper syntax Link to comment https://forums.phpfreaks.com/topic/188005-add-user-script-could-not-execute-query/#findComment-992571 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 Thanks for your help. I tried following the example given but I keep getting unexpected T_ECHO I don't know the proper syntax Why not read the documentation provided? Can't be that hard. $result = mysql_query($sql,$conn) or die("Could not execute query: ". mysql_error()); Link to comment https://forums.phpfreaks.com/topic/188005-add-user-script-could-not-execute-query/#findComment-992589 Share on other sites More sharing options...
okrobie Posted January 11, 2010 Author Share Posted January 11, 2010 I have installed the mysql_error() code, but I'm not getting any error messages from it. Still getting "Could not execute query" I also re-built the table. <html><head><title>Adding a User</title></head> <body> <?php $self = $_SERVER['PHP_SELF']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $user_email = $_POST['user_email']; if( (!$first_name) or (!$last_name) or (!$user_email) ) { $form ="Please enter all new user details..."; $form.="<form action=\"$self\""; $form.=" method=\"post\">First Name: "; $form.="<input type=\"text\" name=\"first_name\""; $form.=" value=\"$first_name\"><br>Last Name: "; $form.="<input type=\"text\" name=\"last_name\""; $form.=" value=\"$last_name\"><br>Email: "; $form.="<input type=\"text\" name=\"user_email\""; $form.=" value=\"$user_email\"><br>"; $form.="<input type=\"submit\" value=\"Submit\">"; $form.="</form>"; echo($form); } else { #connect to MySQL $conn = @mysql_connect("localhost","username","pwd") or die("Could not connect to MySQL"); #select a database $db = @mysql_select_db("dbname",$conn) or die("Could not select database"); #create the SQL query $sql = "insert into tablename (first_name, last_name, user_email) values (\"$first_name\",'\"$last_name\",\"$user_email\" )"; echo mysql_errno($conn) . ": " . mysql_error($conn) . "\n"; #execute the query $result = @mysql_query($sql,$conn) or die("Could not execute query"); if($result) { echo("New user $user_email added"); } echo mysql_errno($conn) . ": " . mysql_error($conn) . "\n"; } ?> </body></html> Link to comment https://forums.phpfreaks.com/topic/188005-add-user-script-could-not-execute-query/#findComment-992900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.