Forum Moderators: coopster

Message Too Old, No Replies

Insert Into TABLE

Insert in TABLE x data

         

emon

9:48 am on Jun 27, 2007 (gmt 0)

10+ Year Member



HELP! I wish insert in table x data... , but always put in next rows.
>>here is my code:

if ($_POST['submit']) {
mysql_pconnect('localhost', 'root', '');
mysql_select_db('test');
$result = mysql_query("SELECT * FROM customer");
$col = mysql_num_fields($result);
for ($j=0;$j<$col;$j++) {
$info = array(mysql_field_name($result,$j));
mysql_query("INSERT INTO customer (".$info['0'].") VALUES ('".$_POST[$j]."')");
}
echo "<meta http-equiv=REFRESH content=0;url=http://".$_SERVER['HTTP_HOST'].">";
} else {
echo "<form method=post action=".$PHP_SELF.">";
mysql_pconnect('localhost', 'root', '');
mysql_select_db('test');
$result = mysql_query("SELECT * FROM customer");
$col = mysql_num_fields($result);
for ($j=0;$j<$col;$j++) {
echo mysql_field_name($result,$j) .": <input type=text name=".$j."><br>";
}
echo "<input type='Submit' name='submit' value='Enter'></form>";
}

Habtom

9:58 am on Jun 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't understand what you are trying to achieve, or what the problem (error) is. For a start, take your connections out of the if conditions:

<?php
mysql_pconnect('localhost', 'root', '');
mysql_select_db('test');

if ($_POST['submit']) {
$result = mysql_query("SELECT * FROM customer");
$col = mysql_num_fields($result);
for ($j=0;$j<$col;$j++) {
$info = array(mysql_field_name($result,$j));
mysql_query("INSERT INTO customer (".$info['0'].") VALUES ('".$_POST[$j]."')");
}
echo "<meta http-equiv=REFRESH content=0;url=http://".$_SERVER['HTTP_HOST'].">";
} else {
echo "<form method=post action=".$PHP_SELF.">";
$result = mysql_query("SELECT * FROM customer");
$col = mysql_num_fields($result);
for ($j=0;$j<$col;$j++) {
echo mysql_field_name($result,$j) .": <input type=text name=".$j."><br>";
}
echo "<input type='Submit' name='submit' value='Enter'></form>";
}

?>

Tell us more about it.

Habtom

emon

10:07 am on Jun 27, 2007 (gmt 0)

10+ Year Member



I wish to insert many fields and values in table...
>> mysql_query("INSERT INTO customer (".$info['0'].") VALUES ('".$_POST[$j]."')") <<
... but I dont know how many fields is in form.

emon

10:33 am on Jun 27, 2007 (gmt 0)

10+ Year Member



Traditional way is this:
$id = $_POST['id'];
$name = $_POST['name'];
$tel = $_POST['tel'];

if ($_POST['submit']) {
mysql_pconnect('localhost', 'root', '');
mysql_select_db('test');
$result = mysql_query("SELECT * FROM customer");
mysql_query("INSERT INTO customer (name,tel) VALUES ('$name','$tel')");
echo "<meta http-equiv=REFRESH content=0;url=http://".$_SERVER['HTTP_HOST'].">";
} else {
echo "<form method=post action=".$PHP_SELF.">Name: <input type=text name=name><br>Tel: <input type=text name=tel><br><input type='Submit' name='submit' value='Enter'></form>";
}

... but I wish to put fields and values name from TABLE(cols name)

Habtom

10:46 am on Jun 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't have to give your input boxes different names, you can just assign it the following way:

This:
for ($j=0;$j<$col;$j++) {
echo mysql_field_name($result,$j) .": <input type=text name=".$j."><br>";
}

can be simply written as:
?>
<input type="text" name="test[]" />
<input type="text" name="test[]" />
<input type="text" name="test[]" />
....
as many as you want.

if you print_r($_REQUEST['test']); you will get the whole values from those boxes in the following format:

Array
(
[0] => box 1
[1] => box 2
[2] => box 3
[3] => box 4
[4] => box 5
)

To insert it into the database, you can just walk through the array.

I hope this helps.

Habtom

emon

11:00 am on Jun 27, 2007 (gmt 0)

10+ Year Member



Field list in form running, but i have problem in this code:
mysql_query("INSERT INTO customer (".$info['0'].") VALUES ('".$_POST[$j]."')");

... how to put all values in this code?

Habtom

11:38 am on Jun 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



array_walk($test, 'insert_values');
function insert_values($row_r, $key) {
mysql_query("INSERT INTO customer (". $field_here .") VALUES ('". $row_r ."')");
}

Aren't your database fields having fixed names? If not, the above code should work well.

Habtom