473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HTML form --> MySQL

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
1 2443
prabirchoudhury
162 100+
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

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

Similar topics

2
by: Mark | last post by:
I have found many examples of how to access an HTML text field from PHP, but cannot find the reverse - how to load an HTML form from a MySQL database. Specifically, I have an HTML form which...
3
by: sammmista | last post by:
hello experts, Plz dont treat this as another newbie query , i did my homework but still getting nowhere :( :( :( Trying to learn PHP on Fedora core 1 (PHP 4.3,MySQL,HTTPD).Unable to post data...
2
by: aseaman | last post by:
I am a big novice with MySQL and PHP. I need a script that enters information from a html form into a MySQL database. Can anyone tell me where I can find one that I can just replace the table and...
6
by: snacktime | last post by:
I've searched and searched and have not found a solution to suppress the margin on form or href tags so that there is no space before or after the tag. The only way I have found to do this is to...
4
by: nathalia | last post by:
Hello, I think i want a simple thing but don't know how to fix it. I have a html form and i want to check if people didn't left any fields blank. If they leave it black i want to show a message...
19
by: Coward 9 | last post by:
HI, I saw in an example hello.aspx, there is a <form tagbeing used like <form runat="server> I search all html tag references and could NOT find "runat" attributes for <formtag. which...
32
by: fjm | last post by:
Hi all, thanks for helping. I'm sure what I want to do is amongst the basic in php. I know ehat I need but I really don't where to begin. What I have is a basic html form I have made consisting of...
1
by: nikhilchandra | last post by:
how clear text field in struts onsubmit event of <html:form> tag
0
by: brianrpsgt1 | last post by:
I am attempting to insert data from a HTML form using a .psp script. I can not find how to link the data that is inserted into the form to the variables in the .psp script to then insert into the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.