By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,069 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,069 IT Pros & Developers. It's quick & easy.

HTML form --> MySQL

P: 41
This should be simple, but I'm stuck

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.  
  8. mysql_select_db('joshdye', $con);
  9.  
  10. $winner = mysql_real_escape_string($_POST['winner']);  
  11. $loser = mysql_real_escape_string($_POST['loser']);  
  12. $num = mysql_real_escape_string($_POST['games']);  
  13.  
  14. $sql=("INSERT INTO tictactoe VALUES (NULL,'$winner,'$loser','$games',NOW())");
  15.  
  16. if (!mysql_query($sql,$con))
  17.   {
  18.   die('Error: ' . mysql_error());
  19.   }
  20. echo "1 record added";
  21.  
  22. mysql_close($con)
  23. ?>
  24.  
  25.  
  26. <form action="<?php echo $SCRIPT_NAME . " method="post">
  27. Winners Name: <input type="text" name="winner" />
  28. Losers Name: <input type="text" name="loser" />
  29. Number of games played: <input type="text" name="games" />
  30. <input type="submit" />
  31. </form>
MySQL Table structure-
Expand|Select|Wrap|Line Numbers
  1. --
  2. -- Table structure for table `tictactoe`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `tictactoe` (
  6.   `ID` int(11) NOT NULL auto_increment,
  7.   `Winner` text NOT NULL,
  8.   `Loser` text NOT NULL,
  9.   `Games` double NOT NULL,
  10.   `When` datetime NOT NULL,
  11.   PRIMARY KEY  (`ID`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  13.  
I spent two days trying to figure this out, but I haven't gotten anywhere
May 6 '09 #1
Share this Question
Share on Google+
1 Reply


prabirchoudhury
100+
P: 162
Hey
I have done some changes on your code and have mentioned the problems . hope that gonna help

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $con = mysql_connect("localhost","root",""); 
  3. if (!$con) 
  4.   { 
  5.   die('Could not connect: ' . mysql_error()); 
  6.   } 
  7.  
  8. mysql_select_db('joshdye', $con); 
  9.  
  10. $winner = mysql_real_escape_string($_POST['winner']);   
  11. $loser = mysql_real_escape_string($_POST['loser']);   
  12. $num = mysql_real_escape_string($_POST['games']);   
  13.  
  14. $sql=("INSERT INTO tictactoe VALUES (NULL,'$winner,'$loser','$games',NOW())");
  15. /******************
  16.  your problems:
  17.  1. youe insert query is wrong
  18.  2. primary key "id" you have declear in the table never be "NULL" value 
  19.  3. you have declear the primary key "id" is a AUTO_INCREMENT , so you dont need to push that
  20.   value in the database all the time. it would take itself as an AUTO_INCREMENT number 
  21.  
  22. */
  23. $sql  = "INSERT INTO tictactoe (Winner, Loser, Games, When ) ";
  24. $sql .= "VALUES ('".$winner."', '".$loser."', '".$num."', '".GETDATE()."') ";
  25.  
  26.  
  27. if (!mysql_query($sql,$con)) 
  28.   { 
  29.   die('Error: ' . mysql_error()); 
  30.   } 
  31. echo "1 record added"; 
  32.  
  33. mysql_close($con) 
  34. ?> 
  35.  
  36.  
  37. <form action="<?php echo $SCRIPT_NAME; ?>" method="post"> 
  38. Winners Name: <input type="text" name="winner" /> 
  39. Losers Name: <input type="text" name="loser" /> 
  40. Number of games played: <input type="text" name="games" /> 
  41. <input type="submit" /> 
  42. </form> 
have a fun

read more
May 6 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.