check against database?

Discussion in 'Programming' started by izlik, Jul 31, 2011.

  1. #1
    i have this html form
    <form action="insert.php" method="post">
    ko: <INPUT NAME="ko" SIZE=7 MAXLENGTH=20 onKeyPress="return numbersonly(this, event)">
    <input type="submit" />
    </form>
    HTML:
    Then i have this php code

    <?
    include "config.php";
    ?>
    
    $sql="INSERT INTO bokning (ko)
    VALUES
    ('$_POST[ko]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "X record added";
    
    mysql_close($con)
    ?>
    PHP:

    let's say the limit for "ko" is set to 3 in the database(Table named "admin", collumn named "komax") and someone enters 5 into the form and presses submit, before it's enterd into the database, how can i make it sum up every row for "ko" in the table, "bokning" and give an error if it exceeds the limit of "komax" ?
     
    Last edited: Jul 31, 2011
    izlik, Jul 31, 2011 IP
  2. Rufas

    Rufas Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #2
    You'll need to query komax from database first, save it as a variable and then do the compare.

    
    <?php
    $name=$_POST['name'];
    $query='SELECT komax FROM admin WHERE item=' . $name;
    $result = mysql_query($query);
    if (!$result) {
        die('Could not query:' . mysql_error());
    }
    $row = mysql_fetch_row($result);
    $komax =$row[0];
    
    $ko=$_POST['ko'];
    if ( $ko <= $komax )
    {
      // your code here
    }
    ?>
    
    Code (markup):
    Change the input for ko into an array. Use
    input name='ko[]'
    Code (markup):
    will do the trick. Then do
    array_sum($_POST['ko'])
    Code (markup):
    to get the total value and continue from there.

    - Rufas
     
    Rufas, Aug 1, 2011 IP