Domanda

I have a form to take statistics for course registration at my university , one of the fields is the "studentid" connecting the form to the database seems to be successful and the primary field with auto increment is working just fine , however entering the student id and submitting the form fails every time and a new record is added with a "0" value ...

this is the html code

     <div class="content">

    <form action="connect-mysql.php" method="POST" enctype="text/plain" id="form3" name="form3" method="post">

    <table width="100%" border="0">
      <tr>
        <td width="50%" bgcolor="#e3e3e3"><center>
        Student ID :
        </center></td>
        <td width="50%" bgcolor="#e3e3e3">
          <span id="sprytextfield1">
          <label for="studentid"></label>
          <input name="studentid" type="text" id="studentid" onblur="MM_validateForm('studentid','','NisNum');return document.MM_returnValue" maxlength="11" />
*<span class="textfieldRequiredMsg">A value is required.</span></span>
        </td>

PHP code

<?php
$host="fdb7.biz.nf" ;`enter code here`
$username="1662822_db1" ;
$password="421343" ;
$db_name="1662822_db1" ;
$tbl_name="courses" ;
    $dbcon = mysqli_connect("$host","$username","$password","$db_name") ;           

    if (!$dbcon) {
    die('error connecting to database'); }

    echo 'you have connected sucessfully' ; 

// escape variables for security
$studentid = mysqli_real_escape_string($dbcon, $_POST['studentid']);

$sql="INSERT INTO courses (studentid)
VALUES ('$studentid')";

if (!mysqli_query($dbcon,$sql)) {
die('Error: ' . mysqli_error($dbcon));
 }
 echo "1 record added"; 
    mysqli_close($dbcon);
  ?>
È stato utile?

Soluzione

method="POST"

is repeated in the form

Check the studentid this way:

$studentid = mysqli_real_escape_string($dbcon, $_POST['studentid']); echo $studentid;

You can print the $sql variable to after setting it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top