HTML form --> MySQL | Member | | Join Date: Aug 2007
Posts: 38
| |
This should be simple, but I'm stuck - <?php
-
$con = mysql_connect("localhost","root","");
-
if (!$con)
-
{
-
die('Could not connect: ' . mysql_error());
-
}
-
-
mysql_select_db('joshdye', $con);
-
-
$winner = mysql_real_escape_string($_POST['winner']);
-
$loser = mysql_real_escape_string($_POST['loser']);
-
$num = mysql_real_escape_string($_POST['games']);
-
-
$sql=("INSERT INTO tictactoe VALUES (NULL,'$winner,'$loser','$games',NOW())");
-
-
if (!mysql_query($sql,$con))
-
{
-
die('Error: ' . mysql_error());
-
}
-
echo "1 record added";
-
-
mysql_close($con)
-
?>
-
-
-
<form action="<?php echo $SCRIPT_NAME . " method="post">
-
Winners Name: <input type="text" name="winner" />
-
Losers Name: <input type="text" name="loser" />
-
Number of games played: <input type="text" name="games" />
-
<input type="submit" />
-
</form>
MySQL Table structure- - --
-
-- Table structure for table `tictactoe`
-
--
-
-
CREATE TABLE IF NOT EXISTS `tictactoe` (
-
`ID` int(11) NOT NULL auto_increment,
-
`Winner` text NOT NULL,
-
`Loser` text NOT NULL,
-
`Games` double NOT NULL,
-
`When` datetime NOT NULL,
-
PRIMARY KEY (`ID`)
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-
I spent two days trying to figure this out, but I haven't gotten anywhere
|  | Familiar Sight | | Join Date: May 2009 Location: Wellington, New Zealand
Posts: 152
| | | re: HTML form --> MySQL
Hey
I have done some changes on your code and have mentioned the problems . hope that gonna help - <?php
-
$con = mysql_connect("localhost","root","");
-
if (!$con)
-
{
-
die('Could not connect: ' . mysql_error());
-
}
-
-
mysql_select_db('joshdye', $con);
-
-
$winner = mysql_real_escape_string($_POST['winner']);
-
$loser = mysql_real_escape_string($_POST['loser']);
-
$num = mysql_real_escape_string($_POST['games']);
-
-
$sql=("INSERT INTO tictactoe VALUES (NULL,'$winner,'$loser','$games',NOW())");
-
/******************
-
your problems:
-
1. youe insert query is wrong
-
2. primary key "id" you have declear in the table never be "NULL" value
-
3. you have declear the primary key "id" is a AUTO_INCREMENT , so you dont need to push that
-
value in the database all the time. it would take itself as an AUTO_INCREMENT number
-
-
*/
-
$sql = "INSERT INTO tictactoe (Winner, Loser, Games, When ) ";
-
$sql .= "VALUES ('".$winner."', '".$loser."', '".$num."', '".GETDATE()."') ";
-
-
-
if (!mysql_query($sql,$con))
-
{
-
die('Error: ' . mysql_error());
-
}
-
echo "1 record added";
-
-
mysql_close($con)
-
?>
-
-
-
<form action="<?php echo $SCRIPT_NAME; ?>" method="post">
-
Winners Name: <input type="text" name="winner" />
-
Losers Name: <input type="text" name="loser" />
-
Number of games played: <input type="text" name="games" />
-
<input type="submit" />
-
</form>
have a fun read more |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|