Jump to content

POST multiple columns of data to one row


lukep11a

Recommended Posts

Hi, I am trying to post 2 selections from radio buttons into 1 row of a mySQL table, this is my code:

 

<form name="form1" method="post" action="new1.php">
<h2>Section 1</h2>
<input type="radio" name="section1" value="Man Utd">Man Utd<br>
<input type="radio" name="section1" value="Chelsea">Chelsea<br>
<input type="radio" name="section1" value="Man City">Man City<br>
<input type="radio" name="section1" value="Arsenal">Arsenal<br>
<h2>Section 2</h2>
<input type="radio" name="section2" value="Liverpool">Liverpool<br>
<input type="radio" name="section2" value="Tottenham">Tottenham<br>
<input type="radio" name="section2" value="Everton">Everton<br>
<input type="radio" name="section2" value="Fulham">Fulham<br>
    <br>
    <input type="submit" value="Submit Team Selections">
</form>

 

on submit, the following code is run:

 

$section1 = $_POST['section1'];
$section2 = $_POST['section2'];
$query="INSERT INTO selections (section1)VALUES ('".$section1."')";
$query="INSERT INTO selections (section2)VALUES ('".$section2."')";
mysql_query($query) or die ('Error updating database');

 

it worked fine when i tested with only one selection, but after trying with 2 selections i found that only the second selection was posted to the mySql table. Does anybody know why this is? Any help would be very much appreciated.

 

Thanks

Luke

You can't run two queries with one call to mysql_query(). In this case there isn't even a reason to run two queries. Validate that both values are present and valid and run one query to insert them.

 

$query = "INSERT INTO selections ( section1, section2 ) VALUES ( '$section1', '$section2' )";

when you specify your $query value twice, you are actually overriding the first value of $query. I recommend doing something like this.

$section1 = $_POST['section1'];
$section2 = $_POST['section2'];
$query="INSERT INTO selections (section1, section2) VALUES ('$section1', '$section2')";
mysql_query($query) or die ("Error updating database: " . mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.