Jump to content

PHP FORM INSERT MYSQL


tarleton

Recommended Posts

Hi guys got a form I am making. At the moment what happens is the user runs the install script then enters details in the form which then posts data to formindb.php which inserts in a php table. At the moment everything works fine except the m_dat and m_tim fields do not get sent from the form to the database. Any ideas?

Install

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE module",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }
// Create table
mysql_select_db("module", $con);

$sql = "CREATE TABLE `module`.`form_data` (`your_team` VARCHAR(30) NOT NULL, `opp_team` VARCHAR(30) NOT NULL, `m_dat` VARCHAR(15) NOT NULL, `m_tim` VARCHAR(15) NOT NULL, `g_name` VARCHAR(30) NOT NULL, `result` VARCHAR(15) NOT NULL, `writeup` TEXT NOT NULL) ENGINE = MyISAM;";

// Execute query
mysql_query($sql,$con) or trigger_error("Error in query: $sql <br>Error was:" . mysql_error(),E_USER_ERROR);
mysql_close($con);
?> 

Form

<!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>Match Report</title>
<style type="text/css">
<!--
#match_report table tr td label {
text-align: center;
}
-->
</style>
</head>

<body>
<form id="match_report" name="Match Report" method="post" FORM ACTION="formindb.php">
  <table width="480" border="0">
     <tr>
       <td colspan="2"><center><strong>Match Report</strong></center></td>
     </tr>
     <tr>
       <td width="176">Your Clan Name:</td>
       <td width="288"><input type="text" name="your_team" id="Team1" value="Your Clan Name" /></td>
     </tr>
     <tr>
       <td>Opposing Team:</td>
       <td><input type="text" name="opp_team" id="opp_team" value="Opposing Team Name"/></td>
     </tr>
     <tr>
       <td>Date of Match:</td>
       <td><input name="m_dat" type="text" id="m_dat" value="dd/mm/year" size="13"/></td>
     </tr>
     <tr>
       <td>Time of Match:</td>
       <td><input name="m_tim" type="text" id="m_tim" value="hh:mm PM" size="13"/></td>
     </tr>
     <tr>
       <td>Game Name:</td>
       <td><input type="text" name="g_name" id="g_name" value="Game Played"/></td>
     </tr>
     <tr>
       <td><label>
      
       </label>
     
Result:</td>
       <td><input name="result" type="text" id="result" value="11-3" size="10"/></td>
     </tr>
     <tr>
       <td>Write Up:</td>
       <td><textarea name="writeup" id="writeup" cols="45" rows="5"></textarea></td>
     </tr>
     <tr>
       <td colspan="2"><label>
         <input type="submit" name="submit" id="submit" value="Submit" />
       </label></td>
     </tr>
  </table>

</form>
</body>
</html>

Formindb.php

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


// Create table
mysql_select_db("module", $con);
$your_team = mysql_real_escape_string($_POST['your_team']);
$opp_team = mysql_real_escape_string($_POST['opp_team']);
$m_tim = mysql_real_escape_string($_POST['m_tim']);
$m_dat = mysql_real_escape_string($_POST['m_dat']);
$g_name = mysql_real_escape_string($_POST['g_name']);
$result = mysql_real_escape_string($_POST['result']);
$writeup = mysql_real_escape_string($_POST['writeup']);
$sql="INSERT INTO form_data (your_team, opp_team, m_dat, m_tim, g_name, result, writeup) VALUES
('$your_team','$opp_team','$m_dat','$m_tim','$g_name','$result','$writeup')
";
// Execute query
mysql_query($sql,$con) or trigger_error("Error in query: $sql <br>Error was:" . mysql_error(),E_USER_ERROR);
mysql_close($con);

?> 

 

 

Any help appreciated.

Link to comment
Share on other sites

You seem to have missed a semi-colon after your SQL query.

 

Try replacing:

$sql="INSERT INTO form_data (your_team, opp_team, m_dat, m_tim, g_name, result, writeup) VALUES
('$your_team','$opp_team','$m_dat','$m_tim','$g_name','$result','$writeup')
";

 

With:

$sql="INSERT INTO form_data (your_team, opp_team, m_dat, m_tim, g_name, result, writeup) VALUES
('$your_team','$opp_team','$m_dat','$m_tim','$g_name','$result','$writeup');
";

 

Hope it helps.

Link to comment
Share on other sites

The semi-colon ; is not required when a query is execuited using php (if that was the problem, the whole query would not work, not just two pieces of data.)

 

The posted code works for me and it is unlikely that just two values would not work, especially since you have echoed them in the code that inserts them into the table. Echo $sql to make sure what the whole query contains (there is a slight chance that if register_globals are on and you have something like cookies with those same names that the value won't be what you expect.)

 

How do you know those two values are not working? Perhaps your code that is displaying the information is not working?

 

You should use a mysql DATETIME data type to hold that information anyway. Using separate values and values with those formats will make your queries and code more complicated if not impossible (that date format 02/09/2009 cannot be directly sorted or used in greater-than/less-than comparisons, which a DATETIME data type can.)

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.