SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict ghostme's Avatar
    Join Date
    Feb 2005
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    column validation with php

    In mysql database Iíve a student table with three fields namely CID, COURSETITLE and PID. The PID (PupilsID) field values have been displayed using drop down fields in the form.php page. Now I Ďve succeeded in populating the database with new cidís (CourseIDs) and CourseTitle, budt what I want to do to be able to validate data that is been inputed into the CID(CourseID) to see whether it exist in the table inorder to alert to the visitor of the existing data
    Pls how can I enforce this type of validation

    PHP Code:
    This is  form.php
    <?php
    $db
    =mysql_connect("localhost","root","");
    mysql_select_db("student",$db);
    $result=mysql_query("select distinct PID from courses ORDER BY pid asc");
    $name='';
    while(
    $rows=mysql_fetch_array($result))
    {
    extract($rows);
    $name.="<option value=\"$PID\">$PID</option>";
    }
    ?>
    <form method="post" action="show.php" name="guestbook">
    <table>
    <tr><td>PupilsID<td><select name="pup">
    <?php echo $name?>
    </select>
    <tr><td>CourseID<td><input type="text" name="textcid"><br/>
    <tr><td>CourseTitle<td><input type="text" name="texttitle"><br/>
    <tr><td><input type="submit" value="submit" name="btnsu">
    <td><input type="reset" value="reset" name="btnre">
    </form>
    PHP Code:
    This is  the form handler
    <?php
    $db
    =mysql_connect("localhost","root","");
    mysql_select_db("student",$db);
    if(isset(
    $_POST['btnsu']))
    {
    $db=mysql_connect("localhost","root","");
    mysql_select_db("student",$db);
    $COURSEID=$_POST['textcid'];
    $COURSETITLE=$_POST['texttitle'];
    $PID=$_POST['pup'];
    $sql="INSERT INTO courses (CID,COURSETITLE,PID) VALUES ('$COURSEID','$COURSETITLE','$PID')";
    mysql_query($sql);}

    ?>

  2. #2
    SitePoint Guru enygmadae's Avatar
    Join Date
    Sep 2002
    Location
    Dallas, Tx.
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you'll just have to do another select before you do an insert...something like:

    PHP Code:
     <?php
     $sql
    ="select count(CID) as cid_count from courses where CID='".$_POST['textcid']."'";
     
    $q=mysql_query($sql);
     while(
    $row mysql_fetch_assoc($q)){ $val=$row; }
     if(
    $row[0]>0){ echo "valid category"; }
     
    ?>
    or something....
    PHP News, Views and Community: http://www.phpdeveloper.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •