Insert into database PHP problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Insert into database PHP problem

Post by mrjtfool »

Hi

I'm currently writing a PHP script to insert some data into a database I've created. The data is taken from a HTML form and is passed to this script which in turn inserts it into the relevant tables of my database. Very simple indeed...

Here is the code:

Code: Select all

<?php
$mysqli = mysqli_connect("localhost", "my_username", "my_password", "my_database");
 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
    $sql = "INSERT INTO salesperson (`salesman_no`, `branch_no`, `first_name`, `last_name`)
    VALUES ('".$POST["salesman_no"]."',\'001\','".$POST["firstname"]."','".$POST["lastname"]."');
    INSERT INTO hhc (`hhc_asset_no`, `salesman_no`)
    VALUES ('".$POST["hhc_no"]."','".$POST["salesman_no"]."')
    INSERT INTO printer (`printer_asset_no`, `salesman_no`)
    VALUES ('".$POST["printer_no"]."','".$POST["salesman_no"]."')";
    $res = mysqli_query($mysqli, $sql);
    
    if ($res === TRUE) {
        echo "A record has been inserted.";
    } else {
        printf("Could not insert a record: %s\n, mysqli_error($mysqli));
    }
    
    mysqli_close($mysqli);
}
?>
Once I submit the form on the other page this script runs but I always get this error message:

Parse error: syntax error, unexpected $end in C:\Users\Jamie\Desktop\xampp\htdocs\xampp\Asset Tracking System\insert_Antrim.php on line 24

Does anyone know why I'm getting this? I've been looking at this same code for ages but just cant work it out.

Thanks for any help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Insert into database PHP problem

Post by requinix »

Code: Select all

printf("Could not insert a record: %s\n, mysqli_error($mysqli));
Get yourself an editor that can do syntax highlighting.
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Re: Insert into database PHP problem

Post by mrjtfool »

Ha thanks I knew it would be something simple. Can anyone recommend any editors?

Also is this the correct SQL syntax to insert data into multiple tables?
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Re: Insert into database PHP problem

Post by mrjtfool »

Sorry about the multiple posts. I've changed the above code to:

Code: Select all

<?php
$mysqli = mysqli_connect("localhost", "my_username", "my_password", "my_database");
 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
    $sql = "INSERT INTO salesperson( salesman_no, branch_no, first_name, last_name ) 
        VALUES ('".$_POST["salesman_no"]."', 
        '001', 
        '".$_POST["firstname"]."',
        '".$_POST["lastname"]."'
        )";
    $sql .= "INSERT INTO hhc( salesman_no, hhc_asset_no )
        VALUES ('".$POST["salesman_no"]."',
        '".$_POST["hhc_no"]."'
        );";
    $res = mysqli_query($mysqli, $sql);
    
    if ($res === TRUE) {
        echo "<p>A record has been inserted.</p>
        <b></b>
        <a href='Antrim.php'>Go Back</a>";
    } else {
        printf("Could not insert a record: %s\n", mysqli_error($mysqli));
    }
    
    mysqli_close($mysqli);
}
?>
It now tells me there is an SQL syntax error when the second INSERT command is read, although I can't see anything wrong with it. Im still searching the net for answers but haven't seen anything wrong with my code...
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Insert into database PHP problem

Post by papa »

Code: Select all

 
#    $sql .= "INSERT INTO hhc( salesman_no, hhc_asset_no )
#         VALUES ('".$POST["salesman_no"]."',
#         '".$_POST["hhc_no"]."'
#         );";
Should be

Code: Select all

 
  $sql .= "INSERT INTO hhc( salesman_no, hhc_asset_no )
         VALUES ('".$POST["salesman_no"]."',
        '".$_POST["hhc_no"]."'
       )";
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Re: Insert into database PHP problem

Post by mrjtfool »

Thanks for the reply.

I've tried what you said and it still brings me back an error message. It says there was an unexpected " on one of the lines.

It seems to be where I am using variables containing data from an HTML form. It doesn't like the way I have set it out. Although I'm almost 100% sure this is the correct syntax???

Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Insert into database PHP problem

Post by John Cartwright »

You are trying to append 2 queries into a single string, however mysqli_query() only supports a single query per execution.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Insert into database PHP problem

Post by requinix »

The error is because you didn't separate the two queries with a semicolon. But even if you did, what John said is still correct.
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Re: Insert into database PHP problem

Post by mrjtfool »

OK, so best thing to do is two seperate myqli_query() calls with one query in each?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Insert into database PHP problem

Post by John Cartwright »

Yes
webnhance
Forum Newbie
Posts: 5
Joined: Tue Feb 10, 2009 6:23 pm

Re: Insert into database PHP problem

Post by webnhance »

The best editor is editplus

editplus.com

you'll love it
mrjtfool
Forum Newbie
Posts: 11
Joined: Fri Feb 06, 2009 4:36 pm

Re: Insert into database PHP problem

Post by mrjtfool »

Thanks using the seperate mysqli_query calls worked!

I'll check that editor out as well. Currently I'm using Crimson Editor which seems quite nice but doesn't check your code 100%.
Post Reply