Jump to content

[SOLVED] how to use a list/menu in a form


saw

Recommended Posts

I am very new to mysql, thank you in advance for any help you can give.

 

I have just learned how to make a form to input data into a mysql table.

I have several text boxes that input the users text into the table.

 

Name:

Gender:

DOB:

etc

 

I am now trying to replace the text box gender with a list/menu to choose female/male.

 

I have managed to create the dynamic list/menu inside the form, but when I click on the button to insert the record I get an error "column gender can not be null"

 

Any help is very much appreciated

Saw

 

My non working code.

<?php require_once('Connections/Insert.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO `index` (Name, Gender, DOB, Mother, Father, Coat, Color, Vocal) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Name'], "text"),
                       GetSQLValueString($_POST['Sex'], "text"),
                       GetSQLValueString($_POST['DOB'], "text"),
                       GetSQLValueString($_POST['Mother'], "text"),
                       GetSQLValueString($_POST['Father'], "text"),
                       GetSQLValueString($_POST['Coat'], "text"),
                       GetSQLValueString($_POST['Color'], "text"),
                       GetSQLValueString($_POST['Vocal'], "text"));

  mysql_select_db($database_Insert, $Insert);
  $Result1 = mysql_query($insertSQL, $Insert) or die(mysql_error());

  $insertGoTo = "cats.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_Insert, $Insert);
$query_Gender = "SELECT Sex FROM sex";
$Gender = mysql_query($query_Gender, $Insert) or die(mysql_error());
$row_Gender = mysql_fetch_assoc($Gender);
$totalRows_Gender = mysql_num_rows($Gender);
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Name:</td>
      <td><input type="text" name="Name" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Gender</td>
      <td><label>
        <select name="gender" id="gender">
          <?php
do {  
?>
          <option value="<?php echo $row_Gender['Sex']?>"<?php if (!(strcmp($row_Gender['Sex'], $row_Gender['']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Gender['Sex']?></option>
          <?php
} while ($row_Gender = mysql_fetch_assoc($Gender));
  $rows = mysql_num_rows($Gender);
  if($rows > 0) {
      mysql_data_seek($Gender, 0);
  $row_Gender = mysql_fetch_assoc($Gender);
  }
?>
        </select>
      </label></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">DOB:</td>
      <td><input type="text" name="DOB" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Mother:</td>
      <td><input type="text" name="Mother" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Father:</td>
      <td><input type="text" name="Father" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Coat:</td>
      <td><input type="text" name="Coat" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Color:</td>
      <td><input type="text" name="Color" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Vocal:</td>
      <td><input type="text" name="Vocal" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"> </td>
      <td><input type="submit" value="Insert record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
<?php
mysql_free_result($Gender);
?>

Link to comment
Share on other sites

I have managed to create the dynamic list/menu inside the form, but when I click on the button to insert the record I get an error "column gender can not be null"

That's because you're not retrieving anything from the SELECT widget named "Gender"... look at $insertSQL!!!!

Link to comment
Share on other sites

Thank you, But I really have no idea what I am doing. I have only done the one tutorial that taught me how to make the input form.

 

Could someone perhaps show an example of how to make a form that mixes text boxes and a list/menu or help me make the appropriate edit to the above code?

 

I kinda learn by example...thank you again

Link to comment
Share on other sites

Could someone perhaps show an example of how to make a form that mixes text boxes and a list/menu or help me make the appropriate edit to the above code?

I told you precisely what to fix.

 

This field doesn't exist:

GetSQLValueString($_POST['Sex'], "text")

 

Switch it to

GetSQLValueString($_POST['Gender'], "text")

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.