Jump to content

my form data is not making it into my sql table but records are created!


baxt01

Recommended Posts

<?php
2.error_reporting(E_ALL);
3.ini_set("display_errors",1);
4. 
5.    if (isset($_POST['submit'] ) ) {
6.        $host_name  = "localhost";
7.        $database   = "DB name";
8.        $user_name  = "user name";
9.        $password   = "my p/w";
10.          
11.        $db = mysqli_connect( $host_name, $user_name, $password, $database );   
12.          
13.        if (!$db)   
14.        {   
15.            die("ERROR please try reloading or email mike@mnvb.co.uk: " . mysql_error());   
16.        }
17.        
18.        // example of inserting data into that table: 
19.        $sql = "INSERT INTO minis(name, score) " 
20.             . " VALUES( ?, ? )"; 
21.        
22.        $stmt = $db->prepare( $sql ); 
23.        if (!$stmt) 
24.        { 
25.            die("Failed to prepare statement: " . $sql); 
26.        } 
27.        
28.        // Get Values
29.        $name=$_POST['name'];
30.	$score="0";
31.        
32.        $stmt->bind_param("ss", $name, $score); 
33.        
34.        if ( ! $stmt->execute() ) 
35.        { 
36.            die("Execution of bound statement failed: " . $stmt->error); 
37.        }
38.        
39.        echo "Inserted {$stmt->affected_rows} correctly.<hr/>"; 
40.        
41.        $db->close();
42.    }
43.?>
44.<!doctype html>
45.<html>
46.    <head>
47.        <meta charset="utf-8">
48.        <title>Music Cafe Minis</title>
49.    </head>
50.    
51.    <body>
52.    <div align="left">
53.        <form name="minis" action=""  method="post">
54.            <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
55.                <tr>
56.                    <td width="95"><div align="right">Name:</div></td>
57.                    <td width="171"><input type="text" name="name" /></td>
58.                </tr>
59.                
60.		<tr>
61.		<input type="hidden" name="score" value="0">
62.		</tr>
63.		
64.                <tr>
65.                    <td><div align="right"></div></td>
66.                    <td><input name="submit" type="submit" value="Submit" /></td>
67.                </tr>
68.            </table>
69.        </form>
70.        
71.        
72.        
73.    </body>
74.</html> 

hi I have had lots of issues around inserting form data into a MySQL table and extracting it but now I have mostly overcome those issues,

I can now finally say my out put page works so thanks to anyone who helped with that,

I can also say y form almost works,

so my current issue is:

I have built a very simple form to send data to a table in MySQL I have got error reporting turn on, ( I get no errors)

once the form is submitted I see my pre set message that my record was added successfully,

so I look on my output page and have even check my table through phpmyadmin to make sure a record has been created which yes that is correct a record is created however the form data is not inserted instead of sending my form data to MySQL its setting the value which should be a name to 0

please take a look over this coding I'm certain you can see for your self what is supposed to be happening and I hope you can debug it to tell me why it is not happening:

 

 

 

Link to comment
Share on other sites

The php code is correct, unlike html but! that won't make any difference, the only reason name will become 0 is because you are not using type char OR similar for field type, but int or similar instead, and because of this, any attempt to insert string value will result in INT of 0 being inserted instead.

Link to comment
Share on other sites

yes that's exactly it I had not noticed it before but INT is the default field setting in phpmyadmin left myself feel a little silly on this one but hey hoe its all working I can now go on to look at the next step in this project thanks very much

 

I see you said the html is not right yes I know I leaves plenty to be worked on I just threw the basics codes in to make the pages display what I wanted I have to actually merge 2 pages now into a final result

its a kind of point keeping sys I'm making so I want to add the form input field onto the same page as my out put narea and I want to change my output are to be displayed into a asocs array as to put it into a table then I can add another row into this html table to place a check box wich will have an ID of "win" once checked it will do a ++1 on the scoe field in the MySQL table and of course add some styling and tidy up the HTML coding in doing this

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...