SQL and PHP inserting

Discussion in 'Programming' started by passingTime, Sep 18, 2011.

  1. #1
    I am having difficult figuring this out... I've been removing and re-adding code for the last hour.... I didn't think something that is supposed to be this simple would cause me this much trouble.
    It worked one time... Then i refreshed and tried again and it hasn't worked since...

    when i submit no data is inserted and -1 for inserted rows is displayed...


    This is my code:

    <?php

    mysql_connect ("localhost", "root", "") or die ("Couldn't connect to the database");

    mysql_select_db("testing");


    $fieldOne = $_POST['fieldOne'];
    $fieldTwo = $_POST['fieldTwo'];
    $fieldThree = $_POST['fieldThree'];


    $SQL ="INSERT INTO test(fieldOne, fieldTwo, fieldThree) VALUES('$fieldOne', '$fieldTwo', 'fieldThree')";

    $input = mysql_affected_rows();

    echo "$input was inserted";

    mysql_close();

    ?>


    <form name="test" method="post" action="insert.php">

    fieldOne: <input type="text" name="fieldOne" /><br><br>
    fieldTwo: <input type="text" name="fieldTwo" /><br><br>
    fieldThree: <input type="text" name="fieldThree" /><br><br>

    <input type="submit" name="submit" value="submit" />

    </form>
     
    passingTime, Sep 18, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    I'm assuming that this file is named insert.php. Try
    
    <?php 
    
    $conn = mysql_connect ("localhost", "root", "") or die ("Couldn't connect to the database");
    
    mysql_select_db("testing", $conn);
    
    
        $fieldOne = isset($_POST['fieldOne']) ? $_POST['fieldOne'] : 'no fieldOne';
        $fieldTwo = isset($_POST['fieldTwo']) ? $_POST['fieldTwo'] : 'no fieldTwo';
        $fieldThree = isset($_POST['fieldThree']) ? $_POST['fieldThree'] : 'no fieldThree';
    
        $SQL ="INSERT INTO test(fieldOne, fieldTwo, fieldThree) VALUES('$fieldOne', '$fieldTwo', 'fieldThree')";
    
        //this is the line that does the actual insert
        $result = mysql_query($SQL);
    
        if(!$result) die mysql_error($conn);
            
        $input = mysql_affected_rows();
            
        echo "$input was inserted";
    
        mysql_close();
    
    ?>
    
    <form  name="test" method="post" action="insert.php">
    
    fieldOne: <input type="text" name="fieldOne" /><br><br>
    fieldTwo: <input type="text" name="fieldTwo" /><br><br>
    fieldThree: <input type="text" name="fieldThree" /><br><br>
    
    <input type="submit" name="submit" value="submit" />
    
    </form>
    
    PHP:
    I don't know how it worked, since you never sent the query. If it still doesn't work, check the database. If you keep trying to insert the same data, and one oof the fields is indexed as non-duplicate, the insert will fail.

    As a last resort, install Firebug and FirePHP (which you should have already installed if you're developing PHP) and see what's where line by line.
     
    Rukbat, Sep 18, 2011 IP
  3. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    after

    $result = mysql_query($SQL);

    add this line
    echo $SQL . '<br />' .mysql_error();

    so you can see the actual query statement and last error.

    Is the page just returning a blank white page?

    This statement could be the culprit.

    if(!$result) die mysql_error($conn);

    You are telling it to do 2 things but you have no curly braces. Try modifying to this:


    if(!$result) die(mysql_error($conn));

    or

    if(!$result) {
    echo mysql_error($conn);
    die();
    }
     
    fr33lanc3, Sep 18, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    die(mysql_error($conn); is a single statement, it doesn't need braces.

    ($die() is a function that echoes the argument, then exists. But it's one function, not 2.)
     
    Rukbat, Sep 18, 2011 IP
  5. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What you are now saying is not what you code shows.

    Did you try what I showed you?

    You statement is wrong for a couple reasons.

    1. you are telling it to die AND show the error if no result. You have no curly braces for these 2 statements.
    2. you are not echoing mysql_error - so you wont see the error.
    3. you are telling code to die first and then show mysql_error - telling it to die first, it wont do anything beyond that.


    Maybe you are intending to do this: if(!$result) die(mysql_error($conn)); Add parens around the die statement and you will see why the insert is failing.
     
    fr33lanc3, Sep 19, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Sorrry but that's just wrong. die() does both. The function works like
    
    function die($message) {
        echo $message;
        exit;
    }
    
    PHP:

    if(!$result) die(mysql_error($conn)); calls a single function - no curly braces are required.
    I'm not, die() is doing the echo.
    No, die is a function that does two things - echoes a message and exits.

    Oops, a typo flame. I missed that. (It would have been far more productive if you had simply said "You left out the parens around the arg for die()".
     
    Rukbat, Sep 19, 2011 IP