inserting values in a database using php and html

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wenggarcia
    New Member
    • Mar 2007
    • 1

    inserting values in a database using php and html

    please help me..

    kindly see if what's wrong with my code below.. im trying to insert values in a table on my database but the query returned as failed.
    [php]
    html code -my input page

    <form action="addcust omers.php" method="$_POST" >
    Customer ID: <input type="text" name="CustID">< br>
    Name: <input type="text" name="Name"><br >
    Address: <input type="text" name="Address"> <br>
    Credit Limit:<input type="text" name="CreditLim it"><br>
    <input type="submit" value="ADD">

    php code-to process my html code
    <?php

    $id = $_POST['CustID'];
    $name=$_POST['Name'];
    $address=$_POST['Address'];
    $creditlimit=$_ POST['CreditLimit'];
    $con=mysql_conn ect('localhost' ,'root','my_pas sword');
    $db=mysql_selec t_db('hsrc',$co n);
    $query_string=" INSERT INTO customer VALUES ($id,'$name','$ address',$credi tlimit)";
    echo $query_string;
    $query=mysql_qu ery($query_stri ng);
    if($query)
    {
    echo"customers record successfully added";
    }else
    {
    echo"adding of customers record failed";
    }
    ?>
    php code -use to display added values in the table

    $con = mysql_connect(' localhost','roo t','my_password ');
    $db = mysql_select_db ('hsrc',$con);
    $str = "SELECT * FROM customer";
    $query = mysql_query($st r);
    echo("<table border=1>");
    echo("<tr><td>C ustomer ID</td><td>Name</td><td>Address</td><td>CreditLi mit</td></tr>");
    while($val=mysq l_fetch_array($ query))
    {
    echo "<tr><td>" . $val['CustID'] . "</td><td>" . $val['Name'] . "</td><td>" . $val['Address']."</td><td>" . $val['CreditLimit'] . "</td><td>" . $val['DepotID'] . "</td></tr>";

    }
    echo "<table>";
    ?>.[/php]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by wenggarcia
    please help me..

    kindly see if what's wrong with my code below.. im trying to insert values in a table on my database but the query returned as failed.

    html code -my input page

    <form action="addcust omers.php" method="$_POST" >

    Customer ID: <input type="text" name="CustID">< br>
    Name: <input type="text" name="Name"><br >
    Address: <input type="text" name="Address"> <br>
    Credit Limit:<input type="text" name="CreditLim it"><br>

    <input type="submit" value="ADD">

    php code-to process my html code

    <?php

    $id = $_POST['CustID'];
    $name=$_POST['Name'];
    $address=$_POST['Address'];
    $creditlimit=$_ POST['CreditLimit'];

    $con=mysql_conn ect('localhost' ,'root','my_pas sword');
    $db=mysql_selec t_db('hsrc',$co n);
    $query_string=" INSERT INTO customer VALUES ($id,'$name','$ address',$credi tlimit)";
    echo $query_string;
    $query=mysql_qu ery($query_stri ng);

    if($query)
    {
    echo"customers record successfully added";


    }else
    {
    echo"adding of customers record failed";
    }

    ?>
    php code -use to display added values in the table

    $con = mysql_connect(' localhost','roo t','my_password ');
    $db = mysql_select_db ('hsrc',$con);
    $str = "SELECT * FROM customer";
    $query = mysql_query($st r);

    echo("<table border=1>");
    echo("<tr><td>C ustomer ID</td><td>Name</td><td>Address</td><td>CreditLi mit</td></tr>");


    while($val=mysq l_fetch_array($ query))
    {
    echo "<tr><td>" . $val['CustID'] . "</td><td>" . $val['Name'] . "</td><td>" . $val['Address']."</td><td>" . $val['CreditLimit'] . "</td><td>" . $val['DepotID'] . "</td></tr>";

    }

    echo "<table>";


    ?>
    Moved to PHP forum.
    Please make use of the code tags when posting code and make sure you are posting in the correct forum.

    Comment

    • savanm
      New Member
      • Oct 2006
      • 85

      #3
      Insert into html table
      <?php
      $sql = "select * from login";
      $ress = mysql_query($sq l);
      //$res = mysql_fetch_ass oc($ress);
      echo ("<table name='data' height='21' width='24' cellpadding='3' cellspacing='3' align='center' bordercolor='bl ack' border='1' bgcolor='#45509 C'>");
      echo "<tr>";
      echo"<td>Checkb ox</td>";
      echo "<td>UserNa me:</td>";
      echo "<td>Passwo rd</td>";
      echo "</tr>";
      while($row = mysql_fetch_arr ay($ress)){
      echo "<tr>";
      echo "<td><input type='checkbox' name='chkbox' align='center'/></td>";
      echo ("<td >$row[user]</td>");
      echo ("<td >$row[password]</td>");
      echo "</tr>";
      }
      echo "</table>";
      ?> Insert into databse
      For example the button is like this
      -----------------------------------------------
      <td><input type="submit" name="btnIns" value="Insert"> </td>
      For example the textbox is like this is like this
      -----------------------------------------------
      <input type="password" name="userpass" align="center" maxlength="14" size="12"></td>
      <input type="text" name="usertxt" align="center" maxlength="14" size="12"></td>
      if(isset($_POST["btnIns"])
      {
      $usertxt = $_POST["$usertxt"];
      $userpass = $_POST["$userpass"];
      $sql="insert into tablenamae values('$usertx t','$userpass') ;
      mysql_query($sq l);
      }
      savanm: enclose your code within php or code tags!! - moderator

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Extract the exact error message from mysql, then you'll know what the problem is. Just change the insert command into
        Code:
        $query=mysql_query($query_string) 
          or die("INSERT error: ".mysql_error());
        Ronald :cool:

        Comment

        Working...