What's the problem with this code?

Discussion in 'PHP' started by saadi123, Feb 4, 2014.

  1. #1
    Below is the code against which I'm receiving error :
    I don't see any error in the SQL syntax therefore I'm unable to resolve the query. Kindly help me out on this one.

    Thank you.

    
    <!DOCTYPE HTML>
        <html>
            <head>
                <title>
                Test Sign Up
                </title>
            </head>
            <body>
                <?php
                $con = mysqli_connect('localhost', 'root', '', 'my_database');
                if (mysqli_connect_errno())
                {
                    echo "Failed to connect to the database" . mysqli_connect_error();
                }
                else
                {
                 echo "Connected <br>";  
                }
               
                $fullName = $_POST['full_name'];
                $sql = "INSERT INTO 'user_information' " .
                "('First Name') " .
                "VALUES ('$fullName')";
                //echo $sql . "<br>";
                $selectDB = mysqli_select_db($con, 'my_database');
                if (!$selectDB)
                {
                    echo "Database could not be selected ". mysqli_error($con);
                }
                $retval = mysqli_query($con, $sql);
                if (!$retval)
                {
                    Die ('Could not enter data '. mysqli_error($con));
                }
               
                ?>
    
    
            </body>
        </html>
    
    
    
    
    
    Code (markup):

     
    Solved! View solution.
    saadi123, Feb 4, 2014 IP
  2. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    Update
    I updated my SQL code and received the following message.
    'User Name' is the column of the table user_information in which I'm trying to insert the data.

    
    $sql = "INSERT INTO `user_information` " .
                "(`First Name`) " .
                "VALUES ('$fullName')";
    
    Code (markup):
    So I would appreciate if someone explains this behaviour as well.
     
    saadi123, Feb 4, 2014 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Update
    I have resolve the above issue as well however, a new issue has raised its head. The error popping up now is:
    The code includes two different pages. So I'm pasting the code for both the pages.

    test_signup.php

    
    <!DOCTYPE HTML>
        <html>
            <head>
                <title>
                Test Sign Up
                </title>
            </head>
            <body>
                <form action = "test_signup_insert.php" method = "POST" name = "test_signup">
                    Full Name: <input type = "text" name = 'full_name'>
                    User Name: <input type = 'text' name = 'user_name'>
                    <?php
                    $_SESSION['user_name'] = $_POST['user_name']; 
                    ?>
                    Email: <input type = 'text' name = 'email_add'>
                    <input type = "submit" name = "submit">
                </form>
               
               
                <?php
               
               
               
                ?>
    
    
    
            </body>
        </html>
    
    Code (markup):
    And now the code for the second page which is:

    test_signup_insert.php

    
    <!DOCTYPE HTML>
        <html>
            <head>
                <title>
                Test Sign Up
                </title>
            </head>
            <body>
                <?php
                $con = mysqli_connect('localhost', 'root', '', 'my_database');
                if (mysqli_connect_errno())
                {
                    echo "Failed to connect to the database" . mysqli_connect_error();
                }
               
               
                $fullName = $_POST['full_name'];
                $userName = $_POST['user_name'];
                $emailAdd = $_POST['email_add'];
               
                $sql = "INSERT INTO `user_information` " .
                "(`First Name`, `User Name`, `Email`) " .
                "VALUES ('$fullName', '$userName', '$emailAdd')";
                echo $sql . "<br>";
                $selectDB = mysqli_select_db($con, 'my_database');
                if (!$selectDB)
                {
                    echo "Database could not be selected ". mysqli_error($con);
                }
                $retval = mysqli_query($con, $sql);
                if (!$retval)
                {
                    Die ('Could not enter data '. mysqli_error($con));
                }
               
                ?>
    
    
            </body>
        </html>
    
    Code (markup):
    As it can be clearly seen that I've defined the $userName variable but still it keeps displaying the error.
    Any suggestion for this problem?

    Thank you.
     
    saadi123, Feb 4, 2014 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    ThePHPMaster, Feb 4, 2014 IP
  5. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    saadi123, Feb 5, 2014 IP
  6. #6
    Just an observation, but you're using mysqli, why the devil are you building the query as a string mysql_ style? That **** should have been pitched out the window a decade ago; Has NO business being done if you're using mysqli or PDO... that's PREPARE and BINDPARAM's job! ESPECIALLY if you're just going to blindly dump postData into it without any form of sanitation. I would also suggest you STOP wasting memory creating variables for nothing... and there's no reason to be building the query or it's value before you have the database selected.

    Similarly you might as well bite the bullet and use the object model instead of the outdated procedural wrappers.

    Also... hmm... you're 'retval' would be a statement object even if no rows were affected -- not sure you want that result.

    $con = new mysqli('localhost', 'root', '', 'my_database');
    if ($con->connect_error) {
    
    	echo 'Failed to connect to the database' . $con->connect_error);
    	
    } else {
    
    	echo 'Connected<br />';
    	/*
    		you should nest the rest HERE so it doesn't try to run
    		if connection failed!
    		
    		Also you created mysqli with my_database selected -- so you
    		don't need to run $con->select_db here!
    	*/
    		
    	$statement = $con->prepare('
    		INSERT INTO user_information (`first name`) values ( ? )
    	');
    	// Also, it's usually a bad idea to have spaces in field names.
    	
    	$statement->bindParam('s', $_POST['full_name']);
    	$statement->execute();
    	if ($con->affected_rows > 0) {
    		echo $con->affected_rows, ' Rows added<br />';
    	} else echo 'No rows were added!';
    }
    Code (markup):
     
    deathshadow, Feb 5, 2014 IP
    ThePHPMaster likes this.
  7. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    saadi123, Feb 6, 2014 IP