Page 1 of 2

Need help

Posted: Tue Sep 16, 2008 4:26 am
by wolfwood16
hi there, just wondering how will i store a group of textbox values on mysql?

here's the scenario, i have ten textboxes on the first page,

Code: Select all

 
<input type="text" name="first">
<input type="text" name="second">
<input type="text" name="third">
<input type="text" name="fourth">
<input type="text" name="fifth">
<input type="text" name="sixth">
 
and then the user will fill it up. On the second page, store it to database and echo the saved values. Im sorry im still a beginner on array, i still dunno how to do it...please help me

Re: Need help

Posted: Tue Sep 16, 2008 4:40 am
by Ziq
Try to use more substantial subject.

Firstly, creat form

Code: Select all

 
<form action="form_handler.php">
<input type="text" name="first">
<!-- And other //-->
<input type="submit">
</form>
 
Then creat form_handler.php

Code: Select all

 
<?
//  Connect to database
//  Insert data in database
mysql_query('INSERT INTO (first, ...) VALUES ('.$_GET['first'].', ...)');
 
//  Print something
echo 'First: '.$_GET['first'];
?>
 
It's very simplify example without any security or something else.
To connect to database use the mysql_connect() function and use other one. You should read the manual http://php.net/

Re: Need help

Posted: Tue Sep 16, 2008 4:43 am
by wolfwood16
how can i make this on array? what if i have a large group of textboxes up to 50....im sure it's efficient to use in array...but how?

Re: Need help

Posted: Tue Sep 16, 2008 4:47 am
by Ziq
how can i make this on array?
It's easy. You should use [] on the textbox's name.

Code: Select all

 
<input type="text" name="textboxes[]">
<input type="text" name="textboxes[]">
<input type="text" name="textboxes[]">
//  and other
 
If you have many textboxes you should use POST method.

Re: Need help

Posted: Tue Sep 16, 2008 5:53 am
by panic!
Hey ziq, your signature says correct errors so it's 'create' not 'creat'. Hope that helps you :)

Re: Need help

Posted: Tue Sep 16, 2008 7:15 am
by wolfwood16
hmmm is this correct?


on the form page

Code: Select all

 
<input type="text" name="textboxes[$id]">
<input type="text" name="textboxes[$id]">
<input type="text" name="textboxes[$id]">
 
and on the process page

Code: Select all

 
foreach($id as $valueToPost){
     echo $valueToPost."<br />";
}
 

Re: Need help

Posted: Tue Sep 16, 2008 7:23 am
by Ziq
No.

process page must be something like:

Code: Select all

foreach($_POST['textboxes'] as $k=>$v)
{
     echo $k.' =>'.$v."<br />";
}
 
Try to print the $_POST variable. It help you to understand this.

Code: Select all

 
print_r($_POST);
 

Re: Need help

Posted: Tue Sep 16, 2008 7:27 am
by Ziq
And you cannot use this

Code: Select all

 
<input type="text" name="textboxes[$id]">
<input type="text" name="textboxes[$id]">
<input type="text" name="textboxes[$id]">
 
Maybe

Code: Select all

 
<input type="text" name="textboxes[<?=$id?>]">
 
or something like this.

You should experiment to understand.

Re: Need help

Posted: Tue Sep 16, 2008 7:32 am
by wolfwood16
Thank you so much Mr. Ziq :)

now i know that putting [] on the tag's name will make it array.

i've just revised your code to my own coz i just want to echo the values filled by the user :)

Code: Select all

 
 
$id = $_POST[textboxes];
 
 foreach($id as $v)
 {
      echo $v."<br />";
 }
 
later on i will put it on the mysql database :) you've been a great hand to me. God bless sir!

Re: Need help

Posted: Tue Sep 16, 2008 7:57 am
by wolfwood16
hmmm i got a problem again sir...

what if the scenario is now like these

Code: Select all

 
<input type="text" name="name[]">
<input type="text" name="middlename[]">
<input type-"text" name="surname[]">
 
<input type="text" name="name[]">
<input type="text" name="middlename[]">
<input type-"text" name="surname[]">
 
<input type="text" name="name[]">
<input type="text" name="middlename[]">
<input type-"text" name="surname[]">
 
and so on...
 
and when i try to store it to database...

Code: Select all

 
$nameid = $_POST[name];
$middlenameid = $_POST[middlename];
$surnameid = $_POST[surname];
 
foreach($nameid as $k){
 $insertNow = "INSERT INTO $DB(id, namefields, middlenamefields, surnamefields) VALUES
       (
       `$_POST[id]`, `$nameid`, `$middlenameid`, `$surnameid`
       )";
 mysql_query($insertNow, $connString) or die("Ehh-Ehhnnnggg!");
}
 
 
and i got the "Ehh-Ehhnnnggg!" result..... :cry: hmmmmppp :x im lost...................

Re: Need help

Posted: Tue Sep 16, 2008 8:13 am
by Ziq
You again admit the logical error.

Code: Select all

 
$nameid = $_POST[name];
$middlenameid = $_POST[middlename];
$surnameid = $_POST[surname];
 
//  Read documentation about foreach!
foreach($nameid as $key => $value)
{
 $insertNow = "INSERT INTO $DB(id, namefields, middlenamefields, surnamefields) VALUES
       (
       `$_POST[id]`, `$nameid[$key]`, `$middlenameid[$key]`, `$surnameid[$key]`
       )";
 mysql_query($insertNow, $connString) or die("Ehh-Ehhnnnggg!");
//  $nameid is an array. And you cannot print variable due print() echo() or some else
}
 
 

Re: Need help

Posted: Tue Sep 16, 2008 8:39 am
by wolfwood16
sir i did exactly what you've taught me, but still it doesn't work.

that was also the exact code of my work...but still... :cry:

Re: Need help

Posted: Tue Sep 16, 2008 9:16 am
by Ziq
Ok... I try again :)

First, replace line

Code: Select all

 
mysql_query($insertNow, $connString) or die("Ehh-Ehhnnnggg!");
 
on

Code: Select all

 
mysql_query($insertNow, $connString) or die(mysql_error());
 
What are you getting in output?

Re: Need help

Posted: Tue Sep 16, 2008 9:25 am
by wolfwood16
Error is

Code: Select all

 
Unknown column '' in 'field list'
 
is the error cause by not filling up the textboxes?...i did filled them

Re: Need help

Posted: Tue Sep 16, 2008 9:40 am
by Ziq
It's strange...

What If you change $insertNow

Code: Select all

 
$insertNow = 'INSERT INTO '.$DB.' (id, namefields, middlenamefields, surnamefields) VALUES ('.$_POST['id'].', '.$nameid[$key].', '.$middlenameid[$key].', '.$surnameid[$key].')';
 
Get sure that $DB exist.

And print the $insertNow in foreach

Code: Select all

 
mysql_query($insertNow, $connString) or die($insertNow.' <br> '.mysql_error());