Solved

Checkboxes for row-insert on my database

Posted on 2009-04-14
9
327 Views
Last Modified: 2012-05-06
Hey I have this code here, that works pretty good. But what I would like is to add some checkboxes that you could click. If you clicked checkbox 2 and 3, it would then add $query = "INSERT INTO added_items (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username')"; into both added_items2 and added_items3
But I can't really seem to figure out how to do that. Its like my mind is completely blank here!
Would it be something like this:

IF checkbox 1 selected $query = "INSERT INTO added_items (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username')";
IF checkbox 2 selected $query = "INSERT INTO added_items (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username')";
IF checkbox 3 selected $query = "INSERT INTO added_items (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username')"; ?

Please help! Thanks ! :)
0
Comment
Question by:MisterHamper
  • 6
  • 3
9 Comments
 

Author Comment

by:MisterHamper
ID: 24139857
This is how my script looks
<?php

if(isset($_POST['add']))

{

include 'config.php';
 

$connection = mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());

 

$db = mysql_select_db($db_name,$connection) or die(mysql_error());
 

$name = mysql_real_escape_string($_POST['name']);

$quantity = mysql_real_escape_string($_POST['quantity']);

$amount = mysql_real_escape_string($_POST['amount']);

$username = mysql_real_escape_string($_SESSION['user_name']);
 

$query = "INSERT INTO added_items1 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username')";

mysql_query($query) or die('There was an error. Please try again.');
 

echo "'$name' have been added to your list";

}

else

{

?>
 

<form method="post">

<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="100"><b>Name:</b></td>

<td><input name="name" type="text" id="name" maxlength="40"></td>

</tr>

<tr>

<td width="100"><b>Amount:</b></td>

<td><input name="amount" type="text" id="amount" maxlength="3"></td>

</tr>

<tr>

<td width="100"><b>Quantity:</b></td>

<td><input name="quantity" type="text" id="quantity" maxlength="2"></td>

</tr>

<tr>

<td width="100">&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<INPUT TYPE=CHECKBOX NAME="items1">items1<BR>

<INPUT TYPE=CHECKBOX NAME="items2">items2<BR>

<INPUT TYPE=CHECKBOX NAME="items3">items3<BR>

<tr>

<td width="100">&nbsp;</td>

<td><input name="add" type="submit" id="add" value="Add New item"></td>

</tr>

</table>

</form>

<?php

}

?>

Open in new window

0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 24140196
That is indeed how its done.
Only one thing that may cause a problem is that with 3 queries like that you should ideally use mysqli_multi_query from mysqli extension.  Having 3 separate queries for each checkbox is straining for the DB.

If you want to stick with old mysql you can run a query every time php detects a selected checkbox:
if(isset($_POST['items1'])){
   $query = " INSERT INTO added_items1 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";
mysql_query($query);
}

if(isset($_POST['items2'])){
   $query = " INSERT INTO added_items2 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";
mysql_query($query);
}

If you want to use better mysqli extension then its cleaner:

$query = '';
if(isset($_POST['items1'])){
   $query .= " INSERT INTO added_items1 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";
}

if(isset($_POST['item2'])){
   $query .= " INSERT INTO added_items2 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";
}

if(isset($_POST['item3'])){
   $query .= " INSERT INTO added_items3 (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";
}

mysqli_multi_query($connection, $query);
0
 

Author Comment

by:MisterHamper
ID: 24140416
Thanks alot for that! It seems smart with the better Mysqli extension, so I just tried that.

It gives me this error here though.
SQL server version for the right syntax to use near 'WHERE username=\'Hamper\'' at line 1

It worked fine previously when there was no check-boxes

Maybe I have done something wrong? Could you take a look? Thanks! Else I will just try to go with the original script you posted
<?php

if(isset($_POST['add']))

{

include 'config.php';
 

$connection = mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());

 

$db = mysql_select_db($db_name,$connection) or die(mysql_error());
 

$name = mysql_real_escape_string($_POST['name']);

$quantity = mysql_real_escape_string($_POST['quantity']);

$amount = mysql_real_escape_string($_POST['amount']);

$username = mysql_real_escape_string($_SESSION['user_name']);
 

$query = '';

if(isset($_POST['items1'])){

   $query .= " INSERT INTO added_breakfast (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}
 

if(isset($_POST['items2'])){

   $query .= " INSERT INTO added_lunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}
 

if(isset($_POST['items3'])){

   $query .= " INSERT INTO added_brunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}
 

if(isset($_POST['items4'])){

   $query .= " INSERT INTO added_brunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}
 

mysqli_multi_query($connection, $query);
 

echo "'$name' have been added to your list";

}

else

{

?>
 

<form method="post">

<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="100"><b>Name:</b></td>

<td><input name="name" type="text" id="name" maxlength="40"></td>

</tr>

<tr>

<td width="100"><b>Kcal:</b></td>

<td><input name="amount" type="text" id="amount" maxlength="3"></td>

</tr>

<tr>

<td width="100"><b>Priority:</b></td>

<td><input name="quantity" type="text" id="quantity" maxlength="2"></td>

</tr>

<tr>

<td width="100">&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

</tr>

<td width="100">&nbsp;</td>

<td><INPUT TYPE=CHECKBOX NAME="items1">items1<BR>

<INPUT TYPE=CHECKBOX NAME="items2">items2<BR>

<INPUT TYPE=CHECKBOX NAME="items3">items3<BR>

<INPUT TYPE=CHECKBOX NAME="items4">items4<BR>

<br><input name="add" type="submit" id="add" value="Add New item"></td>

</tr>

</table>

</form>

<?php

}

?>

Open in new window

0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 24140888
the rest of the code needs readjusting to fit mysqli extension.  While i do that, could you please provide versions of your php and mysql as well as check that mysqli extension is enabled using phpinfo();
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:MisterHamper
ID: 24140927
Oh wow, thanks alot :-)

Yeah sure. PHP version is 5.2.9, MySQL is 5.2.9, phpMyAdmin is 2.11.9.4
I couldn't quite see where phpinfo(): would be. I am using BlueHost as my hoster, they have a cPanel.
0
 

Author Comment

by:MisterHamper
ID: 24140931
MySQL is 5.0.75-community-log, I mean
0
 
LVL 11

Accepted Solution

by:
AlexanderR earned 500 total points
ID: 24140942
create a new php file and put
<?php
phpinfo();
?>
in it.  browse to it and it should give all info you need.

so far numbers look good.  Assuming mysqli is enabled here's the remade code.
<?php

if(isset($_POST['add']))

{

include 'config.php';

 

$connection = mysqli_connect($server, $dbusername, $dbpassword, $db_name) or die(mysql_error());

 

$name = mysqli_real_escape_string($connection, $_POST['name']);

$quantity = mysqli_real_escape_string($connection, $_POST['quantity']);

$amount = mysqli_real_escape_string($connection, $_POST['amount']);

$username = mysqli_real_escape_string($connection, $_SESSION['user_name']);

 

$query = '';

if(isset($_POST['items1'])){

   $query .= " INSERT INTO added_breakfast (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}

 

if(isset($_POST['items2'])){

   $query .= " INSERT INTO added_lunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}

 

if(isset($_POST['items3'])){

   $query .= " INSERT INTO added_brunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}

 

if(isset($_POST['items4'])){

   $query .= " INSERT INTO added_brunch (name, quantity, amount, username) VALUES ('$name', '$quantity', '$amount', '$username');";

}

 

mysqli_multi_query($connection, $query);

 

echo "'$name' have been added to your list";

}

else

{

?>

 

<form method="post">

<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="100"><b>Name:</b></td>

<td><input name="name" type="text" id="name" maxlength="40"></td>

</tr>

<tr>

<td width="100"><b>Kcal:</b></td>

<td><input name="amount" type="text" id="amount" maxlength="3"></td>

</tr>

<tr>

<td width="100"><b>Priority:</b></td>

<td><input name="quantity" type="text" id="quantity" maxlength="2"></td>

</tr>

<tr>

<td width="100"> </td>

<td> </td>

</tr>

<tr>

</tr>

<td width="100"> </td>

<td><INPUT TYPE=CHECKBOX NAME="items1">items1<BR>

<INPUT TYPE=CHECKBOX NAME="items2">items2<BR>

<INPUT TYPE=CHECKBOX NAME="items3">items3<BR>

<INPUT TYPE=CHECKBOX NAME="items4">items4<BR>

<br><input name="add" type="submit" id="add" value="Add New item"></td>

</tr>

</table>

</form>

<?php

}

?>

Open in new window

0
 

Author Comment

by:MisterHamper
ID: 24141044
It seems mysqli extension was enabled, because it working ABSOLUTELY WONDERFULLY!

Thanks alot, my good sir!! :-) Have a nice day
0
 

Author Closing Comment

by:MisterHamper
ID: 31569985
Thanks alot!!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 35
mysqli 3 22
html input type 3 22
.htaccess 5 12
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now