HTML form --> MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpenguin
    New Member
    • Aug 2007
    • 41

    HTML form --> MySQL

    This should be simple, but I'm stuck

    Code:
    <?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-
    Code:
    --
    -- 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
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    HTML form --&gt; MySQL insert problem

    Hey
    I have done some changes on your code and have mentioned the problems . hope that gonna help

    Code:
    <?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
    Last edited by r035198x; May 6 '09, 09:22 AM. Reason: No adverts in technical forums please

    Comment

    Working...