daveh33 Posted October 19, 2007 Share Posted October 19, 2007 <?php session_start(); include("dbconnect.php"); $username = $_POST['UserName']; if (!$username) { echo "You Must enter a username<br>"; $problem = "error"; } else { $query = mysql_query("SELECT * from TABLENAME WHERE username ='$username'") or die(mysql_error()); $result = mysql_query($query); $row = mysql_fetch_array($query) or die(mysql_error()); $numrows = mysql_num_rows($query); echo $numrows; $dbusername = $row['username']; } if ($dbusername) { echo "Sorry that username is not available, please go back and try again.<br>"; $problem = "error"; } else { $password = $_POST['UserPassword']; if (!$password) { echo "You Must enter a password<br>"; $problem = "error"; } $dob = $_POST['UserDob']; if (!$dob) { echo "You Must enter Your date of birth<br>"; $problem = "error"; } $email = $_POST['UserEmail']; $mobile = $_POST['UserMobile']; if (!$mobile) { echo "You Must enter your mobile number<br>"; $problem = "error"; } if ($problem=="error") { echo "<br><b>Your registration was not sucessful</b><br>"; } else { mysql_query("INSERT INTO registration (username, password, age, mobile, email) VALUES ('$username', '$password', '$dob', '$mobile', '$email')") or die(mysql_error()); echo "<p>Registration sucessful, welcome to our site $username!"; } } ?> I get the error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1 Can anyone see whats wrong?? ??? ??? ??? Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/ Share on other sites More sharing options...
pocobueno1388 Posted October 19, 2007 Share Posted October 19, 2007 Your performing a mysql_query() twice. Change this $query = mysql_query("SELECT * from TABLENAME WHERE username ='$username'") or die(mysql_error()); $result = mysql_query($query); To this $query = "SELECT * from TABLENAME WHERE username ='$username'"; $result = mysql_query($query)or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373200 Share on other sites More sharing options...
daveh33 Posted October 19, 2007 Author Share Posted October 19, 2007 Ah yes - that was silly of me. When I change that I get the below error : - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in processregistration.php on line 11 What is wrong with line 11 of the code $row = mysql_fetch_array($query) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373206 Share on other sites More sharing options...
pocobueno1388 Posted October 19, 2007 Share Posted October 19, 2007 Change this line $row = mysql_fetch_array($query) or die(mysql_error()); To $row = mysql_fetch_array($result)or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373207 Share on other sites More sharing options...
daveh33 Posted October 19, 2007 Author Share Posted October 19, 2007 OK I changed that - now when I run the script it prints nothing to the screen.. I know my form works and the variables contain data - what would be stopping the script from processing? Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373211 Share on other sites More sharing options...
pocobueno1388 Posted October 19, 2007 Share Posted October 19, 2007 You had quite a few errors in your code. See if this works for you. <?php session_start(); include("dbconnect.php"); if (!isset($_POST['UserName'])) { echo "You Must enter a username<br>"; $problem = "error"; } else { $username = $_POST['UserName']; $query = "SELECT * from TABLENAME WHERE username ='$username'"; $result = mysql_query($query)or die(mysql_error()); if (mysql_num_rows($result) > 0) { echo "Sorry that username is not available, please go back and try again.<br>"; $problem = "error"; } else { if (!isset($_POST['UserPassword'])) { echo "You Must enter a password<br>"; $problem = "error"; } if (!isset($_POST['UserDob'])) { echo "You Must enter Your date of birth<br>"; $problem = "error"; } if (!isset($_POST['UserMobile'])) { echo "You Must enter your mobile number<br>"; $problem = "error"; } if ($problem=="error") { echo "<br><b>Your registration was not sucessful</b><br>"; } else { $dob = $_POST['UserDob']; $password = $_POST['UserPassword']; $email = $_POST['UserEmail']; $mobile = $_POST['UserMobile']; mysql_query("INSERT INTO registration (username, password, age, mobile, email) VALUES ('$username', '$password', '$dob', '$mobile', '$email')") or die(mysql_error()); echo "<p>Registration sucessful, welcome to our site $username!"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373229 Share on other sites More sharing options...
daveh33 Posted October 19, 2007 Author Share Posted October 19, 2007 That works! Many thanks for your help - its truly appreciated Link to comment https://forums.phpfreaks.com/topic/73957-solved-error-in-your-sql-syntax-can-anyone-see-whats-wrong/#findComment-373236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.