Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert multiple arrays 1

Status
Not open for further replies.

madanthrax

IS-IT--Management
Sep 15, 2001
123
AT
Hi all,

I have not used arrays before and am having a problem working out how to insert multiple rows into MySQL DB.

This is my test form:
Code:
<form action="insert-answers.php" method="post">
<input type="text" name="answer[1]"> <input type="text" name="aValue[1]"><br>
<input type="text" name="answer[2]"> <input type="text" name="aValue[2]"><br>
<input type="text" name="answer[3]"> <input type="text" name="aValue[3]"><br>
<input type="text" name="answer[4]"> <input type="text" name="aValue[4]"><br>
<input type="text" name="answer[5]"> <input type="text" name="aValue[5]"><br>
<input name="submit" type="submit" />
</form>
From info I have found in this forum I have got this far with the insert-answers.php :
Code:
 <?php virtual ('/include/PHPweb.php'); ?>
<?php 
mysql_select_db($database_PHPweb) or die( "Unable to find database");
$arrayname = $_POST['answer'];
 
foreach($arrayname as $key=>$value)
    {
    echo $key.": ".$value;
$sql = "INSERT INTO Answers (Answer) VALUES 
	('$value')";
	
mysql_query($sql);
    }
mysql_close();
?>
The above inserts 5 rows into the Answers table with of course only the Answer field populated, the rest NULL. I need the aValue to be inserted as well.
I have spent the last few hours researching Multidimensional Arrays and learnt a lot, but have been unable to get anything actually working along the lines I want.

Am I barking up the wrong tree? Is there a simpler way than this to add the other values to the rows?

Any help will be greatfully appreciated.

Anthony

&quot;Nothing is impossible until proven otherwise&quot;
 
Code:
foreach($arrayname as $key=>$value){
 $sql = "
	INSERT 
	INTO Answers 
	(Answer, answerValue) 
	VALUES
	(	'".mysql_escape_string($value)."',
		'".mysql_escape_string($_POST['aValue'][$key])."'	)";
 mysql_query($sql) or die(mysql_error());
}
 
Thanks Jpadie, but the insert does not work.
The page insert-answers.php displays only "answer: ArrayaValue: Array"

Here is my adjusted page code:
Code:
<?php virtual ('/include/PHPweb.php'); ?>
<?php 
mysql_select_db($database_PHPweb) or die( "Unable to find database");
$arrayname = $_POST['answer'];
 
foreach($arrayname as $key=>$value)
 {
 $sql = "INSERT INTO Answers (Answer, aValue) VALUES ('".mysql_escape_string($value)."', '".mysql_escape_string($_POST['aValue'][$key])."')";
mysql_query($sql) or die(mysql_error());
 }
mysql_close();
?>
(Field names are answer and aValue)

Any idea what is wrong?

Anthony

&quot;Nothing is impossible until proven otherwise&quot;
 
sorry but i can't see anywhere in your code that would cause it to display such a message on the screen.

have you tried echoing the query before running it? you can then see whether it as you think it should be.
 
Thanks Jpadie, that fixed it.

Echo confirmed that I am an idiot. Got my insert-answers.php backup file mixed up with my current.

All working fine now thanks to your help.

&quot;Nothing is impossible until proven otherwise&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top