Need Some PHP/MYSQL Help

Discussion in 'PHP' started by Handsofmodder, Nov 1, 2008.

  1. #1
    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):

     
    Handsofmodder, Nov 1, 2008 IP
  2. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #2
    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");
     
    MakeThatDollar, Nov 1, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    $result = mysql_query("SELECT * FROM users");
    PHP:
     
    ads2help, Nov 2, 2008 IP
  4. Handsofmodder

    Handsofmodder Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    Handsofmodder, Nov 2, 2008 IP
  5. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Handsofmodder .. you wouldn't ever make us cry or hurt us, would you? :eek:
     
    keyaa, Nov 2, 2008 IP
  6. Handsofmodder

    Handsofmodder Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    .................;)

    seriously can you please help me?
     
    Handsofmodder, Nov 2, 2008 IP
  7. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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 :)
     
    keyaa, Nov 2, 2008 IP
  8. Handsofmodder

    Handsofmodder Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay, it's not displayinjg the error, but I don't see the new info recorded in the database.
     
    Handsofmodder, Nov 2, 2008 IP
  9. PresFox

    PresFox Active Member

    Messages:
    218
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #9
    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
     
    PresFox, Nov 2, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Nothing wrong with the script. Problem must be at your database. Check the field name and field type.
     
    ads2help, Nov 2, 2008 IP
  11. Handsofmodder

    Handsofmodder Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    What am I looking for for the field name and type?
     
    Handsofmodder, Nov 2, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    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?
     
    ads2help, Nov 2, 2008 IP
  13. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #13
    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
     
    ads2help, Nov 2, 2008 IP