I'm trying to display data from my database, but I keep getting this error Here's the code <?php require "header.php"; ?> <body> <?php function register_form($errors=''){ if($errors){ print 'Correct the following errors...'; print '<br><br>'; print implode('</li><li>',$errors); print '</li></ul>'; } print<<<_HTML_ <form method="POST" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="name"> <br> Your Email: <input type="text" name="email"> <br> Your Username: <input type="text" name="username"> <br> Your Password: <input type="text" name="password"> <br> Confirm Password:<input type="text" name="confirm_password"> <br> Done!: <input type="submit" value="done"> <input type="hidden" name="_submit_check" value="1"> </form> _HTML_; } function check_form(){ $errors= array(); if(strlen($_POST['name'])==0){ $errors[]= 'Enter Your Name'; } if(strlen($_POST['email'])==0){ $errors[]= 'Enter Your Email Address'; } if(strlen($_POST['username'])==0){ $errors[]= 'Enter Your desired username'; } if(strlen($_POST['username'])<4){ $errors[]= 'Username must be at least four characters'; } if(strlen($_POST['username'])>8){ $errors[]= 'Username must be less than eight characters'; } if(strlen($_POST['password'])==0){ $errors[]= 'Enter Your password'; } if(strlen($_POST['password'])<4){ $errors[]= 'Password must be at least four characters'; } if(strlen($_POST['password'])>8){ $errors[]= 'Password must be less than eight characters'; } if(!$_POST['password']=$_POST['confirm_password']){ $errors[]= 'Your passwords do not match'; } return $errors; } function process(){ $mysql=mysql_connect("localhost", "admin", "password"); $db = mysql_select_db('rickroller_d'); $result = mysql_query("SELECT * users"); while($row = mysql_fetch_array($result)){ echo $row['user_name']. " - ". $row['user_id']; echo "<br />"; } } register_form(); process(); ?> <?php require "footer.php"; ?> Code (markup):
Try fixing this line: $result = mysql_query("SELECT * users"); You should have at least FROM in the statement, as far as I know. So something like: $result = mysql_query("SELECT * users FROM thisismytablename");
K, I'm facing a new problem. I want to insert form data into mydatabase, but I keep getting this error Here's the code <?php require "header.php"; ?> <body> <?php function register_form($errors=''){ if($errors){ print 'Correct the following errors...'; print '<li><ul>'; print implode('</li><li>',$errors); print '</li></ul>'; } print<<<_HTML_ <form method="POST" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="name"> <br> Your Email: <input type="text" name="email"> <br> Your Username: <input type="text" name="username"> <br> Your Password: <input type="text" name="password"> <br> Confirm Password:<input type="text" name="confirm_password"> <br> Done!: <input type="submit" value="done"> <input type="hidden" name="_submit_check" value="1"> </form> _HTML_; } function check_form(){ $errors= array(); if(strlen($_POST['name'])==0){ $errors[]= 'Enter Your Name'; } if(strlen($_POST['email'])==0){ $errors[]= 'Enter Your Email Address'; } if(strlen($_POST['username'])==0){ $errors[]= 'Enter Your desired username'; } if(strlen($_POST['username'])<4){ $errors[]= 'Username must be at least four characters'; } if(strlen($_POST['username'])>8){ $errors[]= 'Username must be less than eight characters'; } if(strlen($_POST['password'])==0){ $errors[]= 'Enter Your password'; } if(strlen($_POST['password'])<4){ $errors[]= 'Password must be at least four characters'; } if(strlen($_POST['password'])>8){ $errors[]= 'Password must be less than eight characters'; } if(!$_POST['password']=$_POST['confirm_password']){ $errors[]= 'Your passwords do not match'; } return $errors; } function process(){ $mysql=mysql_connect("localhost", "admin", "password") or die(mysql_error()); $db = mysql_select_db('rickroller_d') or die(mysql_error()); $query = "SELECT * FROM user" or die(mysql_error()); $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); $new_id=$row['user_id']+1; mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES($new_id,$_POST['username'],$_POST['email'],$_POST['password'])"); mysql_close($mysql); print 'Welcome to the community!'; } if($_POST['_submit_check']){ if($form_errors=check_form()){ register_form($form_errors); }else{ process(); } } if(!$_POST['_submit_check']){ register_form(); } ?> <?php require "footer.php"; ?> PHP:
Please change this (line 80) mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES($new_id,$_POST['username'],$_POST['email'],$_POST['password'])"); PHP: to this: mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES( '".mysql_real_escape_string($new_id)."', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['password'])."'"); PHP: oh look, no more SQL injection vulnerabilities! also, it might work now. please check
change mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES( '".mysql_real_escape_string($new_id)."', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['password'])."'"); PHP: to mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES( '".mysql_real_escape_string($new_id)."', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['password'])."'") or die(mysql_error()); PHP: And it will tell you what is wrong
Nothing wrong with the script. Problem must be at your database. Check the field name and field type.
example: if the field type is int, it can't accept non-numerical value. and is the field name correct? anyway have u tried PresFox second code?
I have to admit. i must be blind. There is an error with the code. Notice the values (, the bracket was not closed. mysql_query("INSERT INTO user (user_id,user_name,user_email,user_password) VALUES ( '".mysql_real_escape_string($new_id)."', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['password'])."') "); PHP: This code will do the job