Advertisements

insert data to database using php

Postby ElizaKaye » Mon Apr 08, 2013 7:48 am

Offline
ElizaKaye
Posts: 1
Joined: Mon Apr 08, 2013 5:55 am
Thank you in anticipation for your help.

This is my first attempt of inserting data into a database using php.

There are quite a few problems with this code.

1. The box doesn't show up under the table heading "Breed"
2. The second part of the page doesn't show up. For example "previous/next" to request users to go back or forward.
3. It doesn't insert anything into database
4. I have set up a contact.js script to validate form - but I don't know the best way to connect it with php coding.

Please let me know if you require the other file codes.

Code: Select all
<?php require_once('Connections/conn.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 bears (Weight, ----, Type, Colour, Breed ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                     
                       GetSQLValueString($_POST['Weight'], "text"),
                       GetSQLValueString($_POST['----'], "text"),
                       GetSQLValueString($_POST['Type'], "text"),
                       GetSQLValueString($_POST['Colour'], "text"),
                       GetSQLValueString($_POST['Breed'], "int"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());

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

$colname_rs = "-1";
if (isset($_GET['BearID'])) {
  $colname_rs = $_GET['BearID'];
}
mysql_select_db($database_conn, $conn);
$query_rs = sprintf("SELECT * FROM bears WHERE BearID = %s", GetSQLValueString($colname_rs, "int"));
$rs = mysql_query($query_rs, $conn) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bears List  - Insert</title>
</head>
<script type="text/javascript" src="contact.js"> </script>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table border="1" align="center">
    <tr valign="baseline">
      <td colspan="2" align="left" nowrap="nowrap"><h4><strong>Bears List - Insert</strong>
      <br/>
      <h3>All fields are mandatory</h3></td>
      </td>
    </tr>
   
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>* Weight:</strong></td>
      <td><input type="text" name="Weight" value="" size="15" /></td>
    </tr>
   
   <tr valign="baseline">
     <td nowrap="nowrap" align="right" valign="top"><strong>* Gender</strong></td>
    
      <td><select name="----">
     <option value="" selected></option>
        <option value="female">F</option>
        <option value="male">M</option>
      </select></td>
    </tr>
    
    
    
    <tr valign="baseline">
    <td nowrap="nowrap" align="right"><strong>* Type:</strong></td>
      <td><input type="text" name="Type" value="" size="100" /></td>
    </tr>
   
   
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>* Colour:</strong></td>
      <td><input type="text" name="Colour" value="" size="200" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"><strong>* Breed:</strong></td>
      <td><input type="int" name="Breed" value="" size="11" /></td>
    </tr>
   
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td>
      <input type="submit" value="Insert record" />
      <input type="button" name="button" id="button" value="Return to List" onClick="history.back()"/>
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($rs);
?>