From the below code, if it displays 10 records and if the user changes 'Code' or 'Number' value of 2nd and 5th record, how to insert only that 2nd and 5th record in another separate table. But from my insert query i can insert all 10 records. But i need only two affected records to be inserted. Please help.
include('DB.php');
$sql="SELECT * FROM customer where customer_name='".$q."' ";
$result = mysql_query($sql);
$num_row = mysql_num_rows($result);
$row_count=1;
if($num_row>0)
{
echo "<table >
<tr>
<th>S.No</th>
<th>Name</th>
<th>Code</th>
<th>Number</th>
</tr>";
while($row = mysql_fetch_array($result)) {
$c_name=$row['c_name'];
$c_code=$row['c_code'];
$c_number=$row['c_number'];
echo "<tr>";
echo "<td> $row_count.</td>";
echo "<td><input type='text' name='c_name[]' id='c_name' value='$c_name' readonly /></td>";
echo "<td><input type='text' name='c_code[]' id='c_code' value='$c_code' /></td>";
echo "<td><input type='text' name='c_number[]' id='c_number' value='$c_number' /></td>";
echo "</tr>";
$row_count++;
}
echo "</table>";
}
INSERT:
$c_name=$_POST['c_name'];
$c_code=$_POST['c_code'];
$c_number=$_POST['c_number'];
if(isset($c_code))
{
for($i=0;$i<count($c_code);$i++)
{
{
$insert=mysql_query("INSERT INTO customer_table2(name,code,number)
VALUES ('$c_name[$i]','$c_code[$i]','$c_number[$i]')");
}
}
Do a SELECT query first, to see if the row is already in the original table. If not, add it to the new table.
$c_name=$_POST['c_name'];
$c_code=$_POST['c_code'];
$c_number=$_POST['c_number'];
if(isset($c_code))
{
for($i=0;$i<count($c_code);$i++)
{
// Prevent SQL injection
$name = mysql_real_escape_string($c_name[$i]);
$code = mysql_real_escape_string($c_code[$i]);
$number = mysql_real_escape_string($c_number[$i]);
$select = mysql_query("SELECT COUNT(*) AS c FROM customer WHERE name = '$name' AND code = '$code' AND number = '$number'");
$row = mysql_fetch_assoc($select);
if ($row['c'] == 0) {
$insert=mysql_query("INSERT INTO customer_table2(name,code,number)
VALUES ('$name','$code','$number')");
}
}
}
Related
Below is my code. I have two MySQL database tables, one is "member" and another one is "participants". I would like to display the data in table "member" to my local host page and I did it. However, I cannot insert multiple rows of "member" data by using checkbox to my database table "participants". I am stuck. Any help is much appreciated.
<?php
try {
$con = new PDO("mysql:host=localhost;dbname=kgruum member", "root", "");
$sql = $con->query("SELECT * FROM member");
echo "<table class='info' align='center' border='1'>";
echo "<tr><td width='10'></td>
<td width='10'><b>ID</b></td>
<td width='500'><b>Name</b></td>
<td width='50'><b>Handicap</b></td><tr>";
foreach($sql as $row) {
$ID = $row["ID"];
$Name = $row["Name"];
$Handicap = $row["Handicap"];
echo "<tr>
<td><form method='POST' action='Add participant.php'><input type='checkbox' name='insert[]' value='$ID'></td>
<td>$ID</td>
<td>$Name</td>
<td>$Handicap</td><tr>";
}
echo"</table><div align='center'><input type='image' value='submit' src='add selected button.png' alt='submit Button' onmouseover='this.src='pink add selected button.png'' onmouseout='this.src='add selected button.png'' name='add_btn' id='add_btn'></div><br></form>";
if(isset($_POST['add_btn'])) {
if(!empty($_POST['insert'])) {
foreach($_POST['insert'] as $check) {
$st=$con->prepare("INSERT INTO participants(ID,Name,Handicap) VALUES('$ID','$Name','$Handicap')");
$insert->bindParam('ID',$ID);
$insert->bindParam('Name',$Name);
$insert->bindParam('Handicap',$Handicap);
$st->execute();
}
echo "<script type='text/javascript'>
alert('Successful Insert ! ');
window.location.href = 'Add participant.php';
</script>";
} else {
echo "<script type='text/javascript'>alert('You didn't choose which user you want to insert ! ')</script>";
}
}
} catch(PDOException $e) {
echo "error".$e->getMessage();
}
?>
You are not storing the $Name and $Handicap values in the insert[] array, you only storing the $ID value.
To resolve, do something like this:
<input type='checkbox' name='insert[]' value='$ID|$Name|$Handicap'>
Then:
foreach($_POST['insert'] as $check) {
$values = explode('|', $check);
$ID = $values[0];
$Name = $values[1];
$Handicap = $values[2];
//The rest of your SQL code goes here as you have it...
$check = '';
}
I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>
I got a php form to update MySQL tables. The fetch works perfectly but the updates are not working. This is my form code :
<?php
$sql= "SELECT client.resID AS resID, client.resName AS resName FROM client WHERE client.resID =".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT menu.id AS mid, menu.name AS mname FROM menu WHERE menu.resID =".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$sqlF= "SELECT facilities.id AS fid, facilities.name AS fname FROM facilities WHERE facilities.resID =".$_GET["resID"];
$rsF = mysql_query($sqlF) or die($sqlF."<br/><br/>".mysql_error());
$sqlS= "SELECT services.id AS sid, services.name AS sname FROM services WHERE services.resID =".$_GET["resID"];
$rsS = mysql_query($sqlS) or die($sqlS."<br/><br/>".mysql_error());
// $names array now contains all names
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";
while ($fm = mysql_fetch_array($rsM)) { // loop as long as there are more results
$mnames[] = $fm['mname'];
$mid[] = $fm['mid']; // push to the array
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname' value='{$fm['mname']}' /></td>";
echo "<td>{$fm['id']}<input type='hidden' name='mid' value='{$fm['mid']}' /></td>";
echo '</tr>';
++$i;
print_r($mnames);
}
while ($ff = mysql_fetch_array($rsF)) { // loop as long as there are more results
$fnames[] = $ff['fname'];
$fid[] = $ff['fid']; // push to the array
echo '<tr>';
echo "<td>Facilities :</td>";
echo "<td><input type='text' size='40' name='fname' value='{$ff['fname']}' /></td>";
echo "<td>{$ff['id']}<input type='hidden' name='fid' value='{$ff['fid']}' /></td>";
echo '</tr>';
++$i;
}
while ($fs = mysql_fetch_array($rsS)) { // loop as long as there are more results
$snames[] = $fs['sname'];
$sid[] = $fs['sid']; // push to the array
echo '<tr>';
echo "<td>Services :</td>";
echo "<td><input type='text' size='40' name='sname' value='{$fs['sname']}' /></td>";
echo "<td>{$fs['id']}<input type='hidden' name='sid' value='{$fs['sid']}' /></td>";
echo '</tr>';
++$i;
}
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
?>
This is my post code :
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$mname= $_POST['mname'][$i];
$mid = $_POST['mid'][$i];
$query = "UPDATE menu SET name = '$mname' WHERE id = '$mid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$mname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['fname']);
$i = 0;
while ($i < $size) {
$fname= $_POST['fname'][$i];
$fid = $_POST['fid'][$i];
$query1 = "UPDATE facilities SET name = '$fname' WHERE id = '$fid' LIMIT 1";
mysql_query($query1) or die ("Error in query: $query1");
echo "$fname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['sname']);
$i = 0;
while ($i < $size) {
$sname= $_POST['sname'][$i];
$sid = $_POST['sid'][$i];
$query3 = "UPDATE services SET name = '$sname' WHERE id = '$sid' LIMIT 1";
mysql_query($query3) or die ("Error in query: $query3");
echo "$sname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
I got 'updated' status in post page but nothing is updated in MySQL table. How to solve this problem? Really appreciate your help :D
You're creating multiple inputs with the same name, such as "fname," but you're trying to access them as if they were an array. Rename the fields like the following:
echo "<td><input type='text' size='40' name='mname[]' value='name' /></td>";
too long question but if you but by update status in post page you mean to say it echoed update if i am right it will echo update since you are checking it in while and the condition is true. But have you echoed the posted values in post page check whether you are getting the value proceed this way to find where are you having problems before asking here long questions.
you can use transacrions in mysql if you want all the queries should be executed otherwise none.
Your post page says updated because at the end of your queries you just echo "Updated", there is no condition when it should be printed.
and your data is not updated as you are not specifying any connection variable for mysql_query("query",$conn)
Trying to Update the MySQL Tables but nothing is being updated, I'm sure I'm just not seeing the tiny issue, would love some help. Thanks
So its a trade block for a hockey pool, if you want the player on the trade block then you just check the CHECKBOX in the form and submit and it should change the value in the database to value of "1".
FORM:
echo "<table border='1'>";
echo "<tr><th>NAME</th> <th>POS</th> <th>BLOCK</th></tr>";
$counter = 1;
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo "{$row['f_name']}" . " " . "{$row['l_name']}";
echo "</td><td><input name='pl_id[$counter]' type='hidden' value='{$row['pl_id']}'>";
echo "{$row['pos']}";
echo "</td><td><input name='pos[$counter]' type='hidden' value='{$row['pos']}'>";
echo "<input type='checkbox' name='block[$counter]' size='1' value='1'";
if($row['block'] == '1')
{
echo "checked='checked'";
}
echo "></td></tr>";
$counter++;
}
echo "</table>";
SUBMIT PHP PAGE:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("mbbcom1_fantrax") or die(mysql_error());
$i = 1;
while ($i < 26) {
$block = $_POST['block'][$i];
$pl_id = $_POST['pl_id'][$i];
$query = mysql_query("UPDATE 'players'
SET `block` = '$block'
WHERE `players`.`pl_id` = '$pl_id'");
mysql_query($query);
$i++; }
echo mysql_close();
Remove comma before WHERE
mysql_query("UPDATE 'players'
SET block = '$block'
WHERE players.pl_id = '$pl_id'");
You have a } to less, so the PHP code doesn't run.
You do a while loop and a foreach loop, but you are only closing the for loop.
And of course you don't need a , before the WHERE statement
i try to store the booking booths into database based on user selection, there are 10 check boxes for each booths and user can choose which day they want to reserve booths. For each check box has it own field in database, if user choose booth A01, D1 and D2, when he press reserve button, it will insert value into D1 and D2. But I dont know how to get the checked checkbox value to store in database
my booth interface
http://i.imgur.com/umYcI.gif
my table structure
http://i.imgur.com/vKh6R.gif
My coding
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
}
include('db.php');
if($_REQUEST){
$id = $_REQUEST['search_category_id'];
$query2 = mysql_query("SELECT filenameBig, filename, url FROM eventinfo where eventID ='$id'");
$row = mysql_fetch_array($query2, MYSQL_ASSOC);
if($id == -1)
{
echo "<style type='text/css'>#btn_submit{visibility:hidden}</style>";
}
else{
/*echo "<a href='{$row['url']}'>Click me!</a>";*/
echo "<p><br><img src='{$row['filename']}' alt='' /></p>";
echo "<p></p>";
echo "<p align='right'><a href='$row[filenameBig]' target='_blank'>Click to view large image</a></p>";
echo "<hr size='1'>";
echo "<div style='padding-left:4px;' align='left'><strong>Booths Listing</strong>";
echo "<p></p>";
$query = "select boothAlias, totalDay from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";
$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);
echo "<table border='0' style='width:400px;table-layout:fixed' >";
$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
if($day==0){
echo "<th>Booth</th>";
}else{
echo "<th>D".$day."</th>";
}
}
while($rows = mysql_fetch_array($result2)){
$boothAlias=$rows['boothAlias'];
$totalDay=$rows['totalDay'];
echo "<tr><td>$boothAlias</td>";
for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
echo "<td><input name='day2[]' type='checkbox' value='$day2' /></td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I think that SET type would be good solution for this.
http://dev.mysql.com/doc/refman/5.0/en/set.html