Jump to content

[SOLVED] mysql insert


phorcon3

Recommended Posts

html output:

 

<form method="post" action="test.php">
<input type="hidden" name="do" value="1">


<input type="hidden" name="title[]" value="title1">
<input type="hidden" name="text[]" value="text1">

..code..

<input type="hidden" name="title[]" value="title2">
<input type="hidden" name="text[]" value="text2">

..code..

<input type="submit" value="Submit">

</form>

 

now I dun know how to insert it into my MySQL Databank.

 

I tried:

 

<?php

if(isset($_POST['do']))
{
foreach($_POST['title'] as $title)
{
foreach($_POST['text'] as $text)
{
mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$title', '$text')");
}
}
}
?>

 

but that of course didn't work.

 

does anyone have any ideas/suggestion on how I can solve this problem? I'd appreciate it.

Link to comment
Share on other sites

Try this:

 

<?
if(isset($_POST['title'])){
$title = $_POST['title'];
$text = $_POST['text'];

for($i=0;$i<count($title);$i++){
    $ins_title = trim(mysql_real_escape_string($title[$i]));
    $ins_text = trim(mysql_real_escape_string($text[$i]));
    mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$ins_title', '$ins_text')");
}
}
?>

Link to comment
Share on other sites

it worked!! thanks a bunch!! :D

 

I used this instead:

 

<?php
if(isset($_POST['do']))
{
for($i = 0; $i < $_POST['count']; $i++)
{
$title = $_POST['title_'.$i];
$text = $_POST['text_'.$i];
mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$title', '$text')");
}
}
?>

<form method="post" action="test.php">
<input type="hidden" name="do" value="1">
<input type="hidden" name="count" value="2">

<input type="hidden" name="title_1" value="title1">
<input type="hidden" name="text_1" value="text1">

..code..

<input type="hidden" name="title_2" value="title2">
<input type="hidden" name="text_2" value="text2">

..code..

<input type="submit" value="Submit">

</form>

 

but yours looks a helluva lot better lol ..plus it works with the html code I had before ..thanks!

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.