Jump to content

Forms and Mysql


backinblack

Recommended Posts

I am having a little dificult figuring this out. Hope some on cane help. Still pretty new here.

I am doing a website for a race track and what I am trying to do is when I get the results all the info goes to the database. The form is like this: It has Car#, Drives Name, Points.

 

1. #50    Bob Smith  50pts

2. #20    John Doe    48pts

and so on up 25 fields, but when I send the it, it will only show the last one. It will not send all of them.

I want to have all the car numbers in the car numbers field in the database with the drivers name and points the same way.

 

This is probably simple but I am drawing a blank. Can any one help?

Link to comment
Share on other sites

Form I just put a couple of the fields to keep it short. Only submit the form once and it just puts the last one in the database.

<form action="ptsprocess.php" method="post">
<table width="30%"><tr><td width="5%"><b>Car #:</b> </td><td width="15%"><b>Name:</b></td> <td width="5%"><b>Points:</b></td></tr>
<td><input type="text" name="carnumber"></td><td><input type="text" name="name"></td><td><input type="text" name="points"></td></tr>
<tr><td><input type="text" name="carnumber"></td><td><input type="text" name="name"></td><td><input type="text" name="points"></td></tr>
<tr><td><input type="submit" value="Send Results"></td></tr>
</table>
</form>

 

<?php
	$carnumber = $_POST['carnumber'];
	$name = $_POST['name'];
	$points = $_POST['points'];

$host = "XXXXXXX";
$user = "XXXXXXX";
$password = XXXXXXX";
$database = "XXXXXXX";
$connection = mysql_connect($host, $user, $password)
   or die ("Could not retrieve information");
$db = mysql_select_db("databasename",$connection)
   or die ("Could not make connection");

$query = "INSERT INTO points 
(carnumber, name, points)"  . 
"VALUES ('$carnumber','$name','$points')";

$results=mysql_query($query)
or die (mysql_error());
if ($results);

echo 'Result have been posted into database!';
?>

 

AndyB-- You are correct. That is what is messing things up I think. Trying to figure out the correct way to do this.

Link to comment
Share on other sites

The example form you posted has the same name for different inputs.  You need to have every form input NAMEd differently.  For your case, name them as an array .... input type="text" name="carnumber[]" .. etc. and then you can retrieve the values of the carnumber[] array. Handle the other form variables the same way.

Link to comment
Share on other sites

I am pretty new to this so please bare with me. I have renamed all the form values.

For instance... carnumber[0] name[1] points[3] on the first row of the form input fields and

carnumber[4] name[5] points[6] on the second row of fields. For the array can I just name it anything like this

$carnumber = array('carnumber', 'name', 'points')

Link to comment
Share on other sites

Ok I think I am getting closer. What I have is the six form fields I have carnumber[] name[] points[] etc...

then to process it I have

$query = "INSERT INTO points

values ('$carnumber[0]', '$name[1]', '$april15pts[2]'), ('$carnumber[3]', '$name[3]', '$april15pts[4]')";

 

But by doing it this way I get an error saying: Column count doesn't match value count at row 1

From all the different turtorials that I have read, it looks like there are several ways to do this and I can seem to figure it out.

Link to comment
Share on other sites

This is what I came up for you...this will allow you to submit your records to your db one at a time. So basically you can't add all 25 at once.

<form action="ptsprocess.php" method="post">
<table width="30%">
<tr>
	<td width="5%">
	<b>Car #:</b>
	</td>
	<td width="15%">
	<b>Name:</b></td>
	<td width="5%">
	<b>Points:</b>
	</td>
</tr>
<tr>
	<td>
	<input type="text" name="carnumber">
	</td>
	<td>
	<input type="text" name="name">
	</td>
	<td>
	<input type="text" name="points">
	</td>
</tr>
<tr>
	<td>
	<input type="submit" value="Send Results">
	</td>
</tr>
</table>
</form>

<?php

$host = "XXXXXXX";
$user = "XXXXXXX";
$password = XXXXXXX";
$database = "XXXXXXX";

mysql_connect($host,$user,$password); 
@mysql_select_db($database) or die( "Unable to select database");

$carnumber = $_POST['carnumber'];
$name = $_POST['name'];
$points = $_POST['points'];

$query = "INSERT INTO points VALUES ('''$carnumber','$name','$points')";

$results=mysql_query($query) or die (mysql_error());

if ($results);

echo 'Result have been posted into database!';
?>

Link to comment
Share on other sites

Whew! Got it figured out. Thank you all for the help. AndyB you put me on the right track. Usually when people start throwing out ideas, the light bulb will usually come on for me and can figure it out myself.

 

My next problem though is each week of course the points will be different and when I do that it duplicates the drivers car number and name. If I get stuck I will hit you guys up for more info.

 

THANKS AGAIN!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.