TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-30-2011, 10:45 PM   #1 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default work with multi cheek box to same row

hi guys
i need some help to insert multi cheek box to db in one row.

i make like this
in form.html

<table class="table">
<form action="1.php" method="post">
<tr>


<th class="th">ORDER_DESC </th>

</tr>
<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="CSF"/> CSF </td>
</tr>

<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="LFT"/> LFT </td>
</tr>

<tr>
<td class="td"><input type="checkbox" name="checkbox[]" value="RFR"/> RFR </td>
</tr>

<tr>
<td class="td" ><input type="submit" value="Submit" name="Submit"/>
</td>
</tr>
</form>
</table>


in php:

<?php
$orders = $_POST['checkbox'];
mysql_connect("localhost", "root","") or die ('Error: '. mysql_error());
mysql_select_db("project");

if(count($orders)>0)
{
foreach($orders as $key=>$order)
{
$query="INSERT INTO orders (ORDER_DESC) VALUES ('".$order."' )";
mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
}
echo "Order Successfully Placed";
}
else
echo "No Orders";
?>

when i press submit it insert into data but not in one row

can help ?
destroyerx15 is offline  
Reply With Quote
Old 05-31-2011, 01:18 AM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

That is because you have:

PHP Code:
$query="INSERT INTO orders (ORDER_DESC) VALUES ('".$order."' )";

which when it is looping it is replacing the query. Instead try assemble the query. like this:

PHP Code:
if(count($orders)>0)
{
  $query = 'INSERT INTO orders (ORDER_DESC) VALUES ';
  //you could loop through it
  /*
  foreach($orders as $order)
  {
    $query .= "('".$order."'), ";
  }
  */

  //or just use implode
  $query .= "('".implode("'), ('", $orders)."')";
 
  //if you used the implode function you don't need this next line
  $query = trim(', ', $query).';';
  mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
  echo "Order Successfully Placed";
}

that would create something like this:

SQL Code:
INSERT INTO orders (ORDER_DESC) VALUES ('CSF'), ('LFT'), ('RFR');

which is how you query a mysql database to insert multiple records.

Last edited by tony : 05-31-2011 at 01:24 AM. Reason: a simpler solution
tony is offline  
Reply With Quote
Old 05-31-2011, 11:26 AM   #3 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

it dosent work
it give me error in db
destroyerx15 is offline  
Reply With Quote
Old 06-01-2011, 06:51 AM   #4 (permalink)
The Wanderer
 
Join Date: May 2010
Posts: 19
Thanks: 1
core1024 is on a distinguished road
Default

How is your DB structured?
core1024 is offline  
Reply With Quote
Old 06-01-2011, 03:56 PM   #5 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

i didn't understand what you mean
destroyerx15 is offline  
Reply With Quote
Old 06-01-2011, 06:32 PM   #6 (permalink)
The Wanderer
 
Join Date: May 2010
Posts: 19
Thanks: 1
core1024 is on a distinguished road
Default

Do you want to add the values in separate columns or joined in one column?

For all values joined you can just do something like this:
* * *
foreach($orders as $key=>$order) $orders[$key] = mysql_real_escape_string($order);
$orders = implode(', ', $orders);
$sql = "INSERT INTO orders (ORDER_DESC) VALUES ('$orders')";
mysql_query($sql) or die(mysql_error());
core1024 is offline  
Reply With Quote
Old 06-01-2011, 09:59 PM   #7 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

for join in one column
tanx core is work =)
destroyerx15 is offline  
Reply With Quote
Old 06-01-2011, 10:34 PM   #8 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

i want to retrive the cheek box from db to another form to view

i write like this
my second form

<?php
include("config.php");

$orders = $_POST['checkbox'];
foreach($orders as $key=>$order) $orders[$key] = mysql_real_escape_string($order);
$orders = implode(', ', $orders);
$sql = "INSERT INTO orders (ORDER_DESC) VALUES ('$orders')";
mysql_query($sql) or die(mysql_error());
mysql_close($con)
?>

<table class="table" >
<form action="orders1.php" method="post">

<tr>
<th class="th"> ORDERING_DEPT </th>
</tr>
<tr>
<td class="td"> <input type="text" name="ORDERING_DEPT" /> </td>
</tr>

<tr>
<th class="th"> ORDERING_SERVICE </th>
</tr>
<tr>
<td class="td"> <input type="text" name="ORDERING_SERVICE" /> </td>
</tr>



<tr>
<td class="td"> <input type="text" name="ORDERING_DEPT" /> </td>
</tr>


<td class="td" ><input type="submit" value="Submit" name="Submit"/>
</td>
</tr>

</form>
</table>

and for insert :
<?php
include("config.php");

$result =mysql_query("SELECT ORDER_DESC FROM orders
WHERE ORDER_DESC = 'CSF','LFT'");

$sql="INSERT INTO orders (ORDERING_DEPT,ORDERING_SERVICE)
VALUES
('$_POST[ORDERING_DEPT]','$_POST[ORDERING_SERVICE]')";

echo "<table border='1'>
<tr>
<th>ORDER_DESC</th>
<th>ORDERING_DEPT</th>
<th>ORDERING_SERVICE</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ORDER_DESC'] . "</td>";
echo "<td>" . $row['ORDERING_DEPT'] . "</td>";
echo "<td>" . $row['ORDERING_SERVICE'] . "</td>";
echo "</tr>";
}
echo "</table>";

it give me error for boolen
and when i want to insert the data only shown the cheekbx

can help
destroyerx15 is offline  
Reply With Quote
Old 06-02-2011, 07:03 AM   #9 (permalink)
The Wanderer
 
Join Date: May 2010
Posts: 19
Thanks: 1
core1024 is on a distinguished road
Default

<input type="text" name="ORDERING_DEPT" />
is repeated twice. Not sure if it is on purpose, but you should be careful with this.

SELECT ORDER_DESC FROM orders WHERE ORDER_DESC = 'CSF','LFT'
this should be
SELECT ORDER_DESC FROM orders WHERE ORDER_DESC LIKE '%CSF%' AND ORDER_DESC LIKE '%LFT%'
or if you are completely sure for the content
SELECT ORDER_DESC FROM orders WHERE ORDER_DESC = 'CSF, LFT'
the second variant is faster, but the content have to be exact match.

You should always validate your POST/GET data. If for example someone enters anything quoted there are big chances to blow your inserts. You can just $_POST['ORDERING_DEPT'] = mysql_real_escape_string($_POST['ORDERING_DEPT']); and so on...
core1024 is offline  
Reply With Quote
Old 06-02-2011, 11:53 AM   #10 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

i mistake the order dept

but when i wnt to update the db i want to insert another from order_desc to same cheek box to view but not work
destroyerx15 is offline  
Reply With Quote
Old 06-02-2011, 01:31 PM   #11 (permalink)
The Wanderer
 
Join Date: May 2011
Posts: 9
Thanks: 0
destroyerx15 is on a distinguished road
Default

i do like this for insert
im trying using update but dosent work and is insert to diffrent order not in the same
i make my primary key auto increment

<?php
include("config.php");
$ORDERING_DEPT = $_POST['ORDERING_DEPT']== mysql_real_escape_string($_POST['ORDERING_DEPT']);
$result = mysql_query("select ORDER_DESC from orders where ORDER_DESC='CSF' '%CSF%' AND ORDER_DESC LIKE '%LFT%' ");

$sql = "INSERT INTO orders (ORDERING_DEPT) VALUES ('$ORDERING_DEPT')";


mysql_query($sql) or die(mysql_error());

mysql_close($con)
?>


for my shown data :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Untitled Document</title>

</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("project", $con);

$result = mysql_query("SELECT * FROM orders");


echo "<table class='table' >
<tr>
<th class='th'>order_no</th>
<th class='th'>ORDER_TYPE</th>
<th class='th'>ORDER_DESC</th>
<th class='th'>ORDERING_DEPT </th>
<th class='th'>ORDERING_SERVICE</th>
<th class='th'>SENDING_DATE</th>
<th class='th'>DELIVERING_NO</th>
<th class='th'>DELIVERING_DEPT </th>
<th class='th'>DELIVERING_DATE</th>
<th class='th'>PRODECT_CODE </th>
<th class='th'>ORDER_DOSES </th>
<th class='th'>PROFILE_CODE </th>
<th class='th'>LAB_RESULT</th>
<th class='th'>LOW_END</th>
<th class='th'>HIGH_END </th>
<th class='th'>IMAG_CODE </th>
<th class='th'>RADIOLOGY_RESULT </th>
<th class='th'>VERIFY_NO</th>
<th class='th'>STATUS</th>
<th class='th'>URGENCY_CODE </th>
<th class='th'>MEASUREMENT_UNIT </th>
<th class='th'>MEDICINE_ROUTE</th>
<th class='th'>COMMENT </th>
<th class='th'>staff_id</th>
<th class='th'>pat_id </th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='td' >" . $row['order_no'] . "</td>";
echo "<td class='td' >" . $row['ORDER_TYPE'] . "</td>";
echo "<td class='td' >" . $row['ORDER_DESC'] . "</td>";
echo "<td class='td' >" . $row['ORDERING_DEPT'] . "</td>";
echo "<td class='td' >" . $row['ORDERING_SERVICE'] . "</td>";
echo "<td class='td' >" . $row['SENDING_DATE'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_NO'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_DEPT'] . "</td>";
echo "<td class='td' >" . $row['DELIVERING_DATE'] . "</td>";
echo "<td class='td' >" . $row['PRODECT_CODE'] . "</td>";
echo "<td class='td' >" . $row['ORDER_DOSES'] . "</td>";
echo "<td class='td' >" . $row['PROFILE_CODE'] . "</td>";
echo "<td class='td' >" . $row['LAB_RESULT'] . "</td>";
echo "<td class='td' >" . $row['LOW_END'] . "</td>";
echo "<td class='td' >" . $row['HIGH_END'] . "</td>";
echo "<td class='td' >" . $row['IMAG_CODE'] . "</td>";
echo "<td class='td' >" . $row['RADIOLOGY_RESULT'] . "</td>";
echo "<td class='td' >" . $row['VERIFY_NO'] . "</td>";
echo "<td class='td' >" . $row['STATUS'] . "</td>";
echo "<td class='td' >" . $row['URGENCY_CODE'] . "</td>";
echo "<td class='td' >" . $row['MEASUREMENT_UNIT'] . "</td>";
echo "<td class='td' >" . $row['MEDICINE_ROUTE'] . "</td>";
echo "<td class='td' >" . $row['COMMENT'] . "</td>";
echo "<td class='td' >" . $row['staff_id'] . "</td>";
echo "<td class='td' >" . $row['pat_id'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>









</body>
</html>
destroyerx15 is offline  
Reply With Quote
Old 06-03-2011, 01:37 AM   #12 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

just a note, try putting some formatting/highlight on the code to make it more readable, by writing the code between or at least
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining your World: All About Constants Wildhoney General 15 01-29-2013 12:32 PM
Looking For Long Term Work riscphree The Lounge 0 03-14-2010 05:55 PM
Duble the work on pages Face_Of_Boe Absolute Beginners 3 01-06-2009 04:16 PM
File upload forms dont work for me anymore Orc General 6 05-09-2008 10:38 AM
Host Upgraded - File Upload won't work now gillweb General 7 02-25-2008 03:53 AM


All times are GMT. The time now is 03:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design