Hi,
FYI. I have very very little knowledge of PHP & MYSQL.
i am trying to create a simple webform with insert statement what i am trying to do is that, when i enter a new data into this form this will show the message "1 record is added" and the newly inserted record on the same page. But i am getting error on line "50" in this code that there is something wrong with the SQL insert statement. is there any suggestions??

h1>ADD NEW PRODUCT </h1>
    <table width="395" border="0">
      <tr>
        <td width="167">Unit ass number:</td>
        <td width="218"><form method="post" action="add_new_product.php">
          <input name="unit_ass_products" type="text" id="unit_ass_products" size="35">
        </td>
      </tr>
      <tr>
        <td>Prod Unit number:</td>
        <td>
          <input name="pu_number_products" type="text" id="pu_number_products" size="35">
        </td>
      </tr>
      <tr>
        <td>Product Description</td>
        <td>
          <input name="pu_description_products" type="text" id="pu_description_products" size="35">
        </td>
      </tr>
      <tr>
        <td> </td>
        <td><input type="submit" name="submit" id="submit" value="Submit"></td>
      </tr>
</table>
</form>
<?php

    // Enter the server information here...
    $host = "localhost";
    $user = "root";
    $pass = "";
    $database_name = "phl";

    // create connection to the mysql database...
    $con = mysql_connect($host,$user,$pass);
    if(!$con)
    {
        die('Could not connect: ' . mysql_error()); 
    }

    // select database
    mysql_select_db($database_name,$con);

    echo "database connection established...";

    // perform insert action
    // sql statement 
    $sql = "insert into products (PU_NUMBER, PU_DESC, UNIT_ASS) 
            VALUES('$_POST[pu_number_products]', '$_POST[pu_description_products]', '$_POST[unit_ass_products]')";

    // show error information if something goes wrong.
    if(!mysql_query($sql))
    { die('Error: ' . mysql_error());   }

    echo "1 record added";
    mysql_close($con);
?>

Recommended Answers

All 7 Replies

try changing this

 $sql = "insert into products (PU_NUMBER, PU_DESC, UNIT_ASS) VALUES('$_POST[pu_number_products]', '$_POST[pu_description_products]', '$_POST[unit_ass_products]')";

with this

$sql = "INSERT INTO products (PU_NUMBER, PU_DESC, UNIT_ASS)  VALUES('".$_POST[pu_number_products]."', '".$_POST[pu_description_products]."', '".$_POST[unit_ass_products]."')";

Thanks for help.
Now i am not getting errors now but when i try to insert some data, nothing happens and no data saves into the database..

you should declare first your data to be save in a variable on the top of your code like for pu_number_products you can put a variable name like

    <?php

    $pu_number_products=$_POST['pu_number_products'];


    and so on.......

I am getting this message

 Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\rma\beta1\insert.php on line 10

I think i have a problem with teh database table. because i have done every thing followed alot of tutorials but i am getting the same problem.

i have created a simple insert script with only one table. here is the code.
table:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\rma\beta1\insert.php on line 10

CREATE TABLE PRODUCTS
(
    PU_NUMBER varchar(11) NOT NULL UNIQUE,
    PU_DESC varchar(50) NOT NULL,
    UNIT_ASS VARCHAR(10) NOT NULL,
    PRIMARY KEY (PU_NUMBER)
);

and the insert script

<?php

    $mysqli = mysqli_connect("localhost", "root", "", "test");

    if(mysqli_connect_error()) {
        printf("Connect failed: %s\n",mysqli_connect_error());
        exit();
    } else {
        $sql = "INSERT INTO products (PU_NUMBER, PU_DESC, UNIT_ASS) 
                values($_POST['pu_number'], $_POST['PU_DESC'],$POST['UNIT_ASS'])";
                $res = mysqli_query($mysqli, $sql);
                if($res == TRUE) {
                    echo "A record is added..";
                } else { printf(mysqli_error($mysqli)); }
?>

mysqli_close($mysqli);

}

?>
and i am getting this error when i put some information on it.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\rma\beta1\insert.php on line 10

$sql = "insert into products (PU_NUMBER, PU_DESC, UNIT_ASS)
VALUES('$_POST[pu_number_products]', '$_POST[pu_description_products]', '$_POST[unit_ass_products]')";

Its should be :

$username = $_POST['user_name'];
$password = $_POST['password'];

$sql = "insert into tblusers (user_name, password)
VALUES('$username', '$password')";

It must work..

well by looking at your code i can say that you make a big mess out their try looking at your first post and you can see were you made the mistake and try to rereading your own post,it will help you back in the track..let em give some hint

your missing this part

    $host = "localhost"; 
    $user = "root";
    $pass = "";   
    $database_name = "phl"; //database name

and also change this

 $sql = "INSERT INTO products (PU_NUMBER, PU_DESC, UNIT_ASS) 
                values($_POST['pu_number'], $_POST['PU_DESC'],$POST['UNIT_ASS'])";

with this

$sql = "INSERT INTO products (PU_NUMBER, PU_DESC, UNIT_ASS)  VALUES('".$_POST[pu_number]."', '".$_POST[PU_DESC]."', '".$_POST[UNIT_ASS]."')";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.