Adding data to a mysql database works one time only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basestring
    New Member
    • Mar 2007
    • 3

    Adding data to a mysql database works one time only

    Hi I am busy now for many hours without luck

    I have a database and when I use PHP to add date to it, it works only one time
    when i want to add the next data, It doesn't work. but i don't get any errors???

    see my code:
    [PHP]
    $query = "INSERT INTO products VALUES ('$typedata','$ pro_code_ourdat a','$pronum','$ proname','$prod etails','$facnu m','$picturedat a','$price','$p robox','$proqua ntity')";
    mysql_query($qu ery);
    [/PHP]

    What is wrong here or what is missing?
    Please help

    You can check it at



    Here is the full code:
    Maybe some code is a bit unusual but that is because I'm new with PHP
    [PHP]<html>
    <head>
    <title>Untitl ed Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" >
    </head>

    <body bgcolor="#FFFFF F" text="#000000">
    <?
    $pictureloadok = 0;
    $type = $_REQUEST['type'];
    $pronum = $_REQUEST['pronum'];
    $proname = $_REQUEST['proname'];
    $prodetails = $_REQUEST['prodetails'];
    $probox = $_REQUEST['probox'];
    $proquantity = $_REQUEST['proquantity'];
    $facnum = $_REQUEST['facnum'];
    $price = $_REQUEST['price'];
    If (!empty($type)) {
    $typeok = 1;
    }else{
    $typeok = 0;
    }
    If (!empty($pronum )) {
    $pronumok = 1;
    }else{
    $pronumok = 0;
    }
    If (!empty($pronam e)) {
    $pronameok = 1;
    }else{
    $pronameok = 0;
    }
    If (!empty($prodet ails)) {
    $prodetailsok = 1;
    }else{
    $prodetailsok = 0;
    }
    If (!empty($probox )) {
    $proboxok = 1;
    }else{
    $proboxok = 0;
    }
    If (!empty($proqua ntity)) {
    $proquantityok = 1;
    }else{
    $proquantityok = 0;
    }
    If (!empty($facnum )) {
    $facnumok = 1;
    }else{
    $facnumok = 0;
    }
    If (!empty($price) ) {
    $priceok = 1;
    }else{
    $priceok = 0;
    }
    if ($typeok = 1) {
    if ($pronumok = 1) {
    if ($pronameok = 1) {
    if ($prodetailsok = 1) {
    if ($proboxok = 1) {
    if ($proquantityok = 1) {
    if ($facnumok = 1) {
    if ($priceok = 1) {
    If (!empty($_FILES['uploaded']['name'])){
    $target = "databasepictur es/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $ok=1;
    if(move_uploade d_file($_FILES['uploaded']['tmp_name'], $target)){
    $pictureloadok = 1;
    $picturedata = "/pictures/databasepicture s/".$_FILES['uploadedfile']['name'];
    }else{
    $pictureloadok = 0;
    }
    }else{
    $pictureloadok = 2;
    }
    }
    }
    }
    }
    }
    }
    }
    }
    if ($pictureloadok == 1) {
    $user="*******" ;
    $password="**** ***";
    $database="**** ****";
    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query="SELECT * FROM `products` ";
    mysql_query($qu ery);
    $result=mysql_q uery($query);
    $num=mysql_numr ows($result) ;
    $num--;
    $pro_code_ourda ta=mysql_result ($result,$num," pro_code_our");
    $pro_code_ourda ta++;
    mysql_close();

    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query="SELECT * FROM pro_index ";
    mysql_query($qu ery);
    $result=mysql_q uery($query);
    $num=mysql_numr ows($result);
    $i=0;
    while ($i < $num){
    $typedataa=mysq l_result($resul t,$i,"pro_type_ code");
    $typedatab=mysq l_result($resul t,$i,"pro_type" );
    if ($typedatab == $type) {
    $typedata = $typedataa;
    }
    $i++;
    }

    $picturedata = "products/".$target;
    $query = "INSERT INTO products VALUES ('$typedata','$ pro_code_ourdat a','$pronum','$ proname','$prod etails','$facnu m','$picturedat a','$price','$p robox','$proqua ntity')";
    mysql_query($qu ery);
    mysql_close();
    Print $typedata;
    print " ";
    print $pro_code_ourda ta;
    print " ";
    print $pronum;
    print " ";
    print $proname;
    print " ";
    print $prodetails;
    print " ";
    print $facnum;
    print " ";
    print "products/".$target;
    print " ";
    print $price;
    print " ";
    print $probox;
    print " ";
    print $proquantity;
    print " ";
    print "are added to the database";
    }

    $user="******";
    $password="**** **";
    $database="**** **";
    $pro_type_scree n = isset($_REQUEST['pro_type_scree n']) ? trim($_REQUEST['pro_type_scree n']) : "";
    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query="SELECT * FROM `products` ";
    mysql_query($qu ery);
    $result=mysql_q uery($query);
    $num=mysql_numr ows($result);
    ?>
    <table width="*" border="1" cellspacing="0" cellpadding="0" >
    <?

    $i=0;
    while ($i < $num) {
    $pro_type=mysql _result($result ,$i,"pro_type") ;
    $pro_code_our=m ysql_result($re sult,$i,"pro_co de_our");
    $pro_code_fac=m ysql_result($re sult,$i,"pro_co de_fac");
    $pro_name=mysql _result($result ,$i,"pro_name") ;
    $pro_details=my sql_result($res ult,$i,"pro_det ails");
    $pro_fac=mysql_ result($result, $i,"pro_fac");
    $pro_pic=mysql_ result($result, $i,"pro_pic");
    $pro_price=mysq l_result($resul t,$i,"pro_price ");
    $pro_boxsize=my sql_result($res ult,$i,"pro_box size");
    $pro_inbox=mysq l_result($resul t,$i,"pro_inbox ");
    ?>
    <tr>
    <?
    echo"<td width='20' valign='top'>$p ro_type</td>";
    echo"<td width='75' valign='top'>$p ro_code_our</td>";
    echo"<td width='75' valign='top'>$p ro_code_fac</td>";
    echo"<td width='100' valign='top'>$p ro_name</td>";
    echo"<td width='400' valign='top'>$p ro_details</td>";
    echo"<td width='20' valign='top'>$p ro_fac</td>";
    echo"<td width='400' valign='top'>$p ro_pic</td>";
    echo"<td width='150' valign='top'>$p ro_price</td>";
    echo"<td width='100' valign='top'>$p ro_boxsize</td>";
    echo"<td width='50' valign='top'>$p ro_inbox</td>";
    ?>
    </tr>
    <?
    $i++;
    }
    ?>
    </table>
    <?
    mysql_close();
    ?>
    <form name="form1" ENCTYPE="multip art/form-data" method="post" action="index.p hp">
    <?
    $user="******";
    $password="**** **";
    $database="**** **";
    $pro_id=$_POST['pro_id'];
    $pro_type=$_POS T['pro_type'];
    mysql_connect(l ocalhost,$user, $password);
    @mysql_select_d b($database) or die( "Unable to select database");
    $query="SELECT * FROM pro_index";
    mysql_query($qu ery);
    $result=mysql_q uery($query);
    $num=mysql_numr ows($result);
    $i=0;
    ?>
    Product type
    <select name="type" size="1">
    <?
    while ($i < $num) {
    $pro_id=mysql_r esult($result,$ i,"pro_type_cod e");
    $pro_type=mysql _result($result ,$i,"Pro_type") ;
    echo "<option>$pro_t ype</option>";
    $i++;
    }
    mysql_close();
    ?>
    </select>
    <?
    $pro_code_our++ ;
    Echo "<br>Produc t number = BS$pro_code_our </p>";
    ?>
    <br>
    Product number from Factory
    <input type="text" name="pronum">
    <br>
    Product name
    <input type="text" name="proname">
    <br>
    Product details
    <input type="text" name="prodetail s" size="50">
    <br>
    Box size
    <input type="text" name="probox">
    <br>
    Quantity
    <input type="text" name="proquanti ty">
    <br>
    Picture
    <INPUT NAME="uploaded" TYPE="File">

    <br>
    Factory number
    <input type="text" name="facnum">
    <br>
    Buy Price
    <input type="text" name="price">
    <br>
    <input type="submit" name="Submit" value="Add record">
    <br>
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    [/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What do you mean by
    I have a database and when I use PHP to add date to it, it works only one time when i want to add the next data, It doesn't work. but i don't get any errors???
    1. How do you know it does not work? How did you verify that?

    2. You don't get any errors because you do not trap the error message. Change your statement to
    Code:
    $r=mysql_query($query)
       or die ("INSERT error: ".mysql_error());
    and see what error message is returned.

    Ronald :cool:

    Comment

    • basestring
      New Member
      • Mar 2007
      • 3

      #3
      Greet, I didn't know that.

      I got the massage INSERT error: Duplicate entry '1003' for key 1

      I have 2 rows added with Myphpadmin.
      the 3rd Row was added with my script. but wil add the row at the begining and not at the end. my script will check the last row not the first.

      Thanks for the advise

      Comment

      Working...