PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 4:26 am
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
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 4:40 am
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/
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 4:43 am
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?
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 4:47 am
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.
panic!
Forum Regular
Posts: 516 Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK
Post
by panic! » Tue Sep 16, 2008 5:53 am
Hey ziq, your signature says correct errors so it's 'create' not 'creat'. Hope that helps you
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 7:15 am
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 />";
}
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 7:23 am
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.
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 7:27 am
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.
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 7:32 am
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!
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 7:57 am
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.....
hmmmmppp
im lost...................
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 8:13 am
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
}
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 8:39 am
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...
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 9:16 am
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?
wolfwood16
Forum Commoner
Posts: 43 Joined: Wed Aug 27, 2008 8:52 am
Contact:
Post
by wolfwood16 » Tue Sep 16, 2008 9:25 am
Error is
Code: Select all
Unknown column '' in 'field list'
is the error cause by not filling up the textboxes?...i did filled them
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Tue Sep 16, 2008 9:40 am
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());