mysql comparing two integers from two tables - php

if($num>0) {
echo "<table border=2> Table Request".$_SESSION['s1'];
echo"<tr>
<td>Id</td><td>Drug</td><td>Quantity</td>
</tr>";
for($i=0;$i<$num;$i++) {
$row=mysql_fetch_row($result);
$r[$i]=$row[1];
echo "<tr>";
for($j=0;$j<$num1;$j++) {
echo"<td>$row[$j]</td>";
}
echo"<td><input type='Checkbox' name='p[$i]' value='on' unchecked /></td>";
echo"<td><input type='txt' name='q[$i]' /></td>";
echo"</tr>";
$r[$i]=$row[1];
}
if(isset($_POST['p'])) {
foreach($_POST['p'] as $key=>$value) {
if($value == "on") {
$query8 = "select $r[$i] from $_SESSION['t'] ";
echo $query8;
$result8 = mysql_query($query8);
$num8=Mysql_num_rows($result8);
if($num8!=0) {
$query7="select qun from $_SESSION['t']";
$result7 = mysql_query($query8);
//?????????????????
}
}
echo"</table>";
}
}//result
}//else
I have a table request and another table for example E.
I want to compare the field quantity of these tables
if(select qun from request)<((select qun from $_SESSION['t'])) // some work
How can I write this code in the part that I marked with many question marks?
is this correct?

If you really want what the title says, why you don't do something like that
SELECT table1.quantity AS qu1, table2.quantity AS qu2 FROM table1, table2 WHERE your_conditions;
When you get the results, you can compare qu1 against qu2.
But if you are looking for something different, than please be more specific with your question.
Actual example:
$query8 = 'SELECT '.$_SESSION[ 't'].'.'.$r[$i].' AS qu1, request.qun AS qu2 FROM '.$_SESSION[ 't'].', request';
$result8 = mysql_query($query8);
while ($row8 = mysql_fetch_array($result8)) {
if ($row8[ 'qu1'] < $row8[ 'qu2']) {
echo 'the value from '.$r[$i]. ' is smaller';
} elseif ($row8[ 'qu1'] > $row8[ 'qu2']) {
echo 'the value from '.$r[$i]. ' is bigger';
} else {
echo 'both values are same';
}
}

Related

Inserting multiple rows into MySQL with checkbox

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 = '';
}

Not getting the radio values and score of quiz script

I am making a multiple choice quiz using php and sql. When I play the quiz and submit the answer it not show the score.
Here is the script:
<?php
$con = mysql_connect("localhost", "ashu","ashua");
$db=mysql_select_db("quiz",$con) or die(mysql_error());
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
if (empty($_POST['submit'])) {
echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$answer = $row["answer"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
elseif (!empty($_POST['submit'])) {
$score = 0;
$total = mysql_num_rows($display);
while ($result = mysql_fetch_array($display)) {
$answer = $result["answer"];
$q = $result["q"];
if ($q == $answer) {
$score++;
}
}
echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";
if ($score == $total) {
echo "Congratulations! You got every question right!";
} elseif ($score/$total < 0.34) {
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
} elseif ($score/$total > 0.67) {
echo "Well done! You certainly know your stuff.";
} else {
echo "Not bad - but there were a few that caught you out!";
}
echo "</p>";
echo "<p>Here are the answers:";
echo "<table border=0>";
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
while ($row = mysql_fetch_array($display)) {
$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];
echo "<tr><td><br>$question</td></tr>";
if ($q == $answer) {
echo "<tr><td>»you answered ${$q}, which is correct</td></tr>";
} elseif ($q == "") {
echo "<tr><td>»you didn't select an answer. The answer is $answer</td></tr>";
} else {
echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>";
}
}
echo "</table></p>";
}
?>
when i submit or finish the quiz i get the result as
You scored 0 out of 2
Oh dear. Not the best score, but don't worry, it's only a quiz.
Here are the answers:
Q1:Gaurav
»you didn't select an answer. The answer is 1
Q2:ashuas
»you didn't select an answer. The answer is 12
it shows only correct answer not showing the the correct score and in amswer it alwas shows you didnt select and answer.
You write elseif (!empty($_POST['submit'])), but never access $_POST in the block that follows. Shouldn't you at least use $POST to check whether a posted answer is correct?
Instead, you are checking whether a posted answer is correct using
while ($result = mysql_fetch_array($display)) {
$answer = $result["answer"];
$q = $result["q"];
if ($q == $answer) {
$score++;
}
}
i.e. by looking at the database only.
Here is your Answer Just Replace with your code
<?php
$con = mysql_connect("localhost", "ashu","ashua");
$db=mysql_select_db("quiz",$con) or die(mysql_error());
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
if (empty($_POST['submit'])) {
echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$answer = $row["answer"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
elseif (!empty($_POST['submit']))
{
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
$score = 0;
$total = mysql_num_rows($display);
while ($result = mysql_fetch_array($display))
{
$id = $result["id"];
$q = $_POST["q$id"];
$answer = $result["answer"];
if ($q == $answer)
{
$score++;
}
}
echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";
if ($score == $total) {
echo "Congratulations! You got every question right!";
}
elseif ($score/$total < 0.34) {
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
}
elseif ($score/$total > 0.67) {
echo "Well done! You certainly know your stuff.";
}
else {
echo "Not bad - but there were a few that caught you out!";
}
echo "</p>";
echo "<p>Here are the answers:";
echo "<table border=0>";
$display = mysql_query("SELECT * FROM quiz ORDER BY id");
while ($row = mysql_fetch_array($display)) {
$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];
echo "<tr><td><br>$question</td></tr>";
if ($q == $answer)
{
echo "<tr><td>»you answered ${$q}, which is correct</td></tr>";
}
elseif ($q == "") {
echo "<tr><td>»you didn't select an answer. The answer is $answer</td> </tr>";
}
else {
echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>";
}
}
echo "</table></p>";
}
?>

Insert that particular value changed record

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')");
}
}
}

Populating select input field with value from mysql

My form has input fields with values populated from mysql table. In my select statement I am passing these values to the fields. The table is called person and has a unique id person_id and foreign key academy_id. Each person has a status active or inactive stored in field name person_status. I am having difficulties pulling the values from person_status for each person. How would I show the status for each person inside the select query? EXAMPLE
Select query to populate
<?php
$id = 15;
$db_select2 = $db_con->prepare("
SELECT a.name,
a.academy_id,
p.person_id,
p.person_status,
p.first_name
FROM academy a
LEFT JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :id
");
if (!$db_select2) return false;
if (!$db_select2->execute(array(':id' => $id))) return false;
$results2 = $db_select2->fetchAll(\PDO::FETCH_ASSOC);
if (empty($results2)) return false;
$result2 = '';
$s = 1;
echo "<table>";
echo "<tbody>";
foreach ($results2 as $value2){
echo "<tr>";
echo "<td>Name #".$s."<input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."' readonly=\"readonly\"/><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #".$s." First Name\" type=\"text\" value='" . $value2['first_name'] ."'/></td>";
echo "</tr>";
$s++;
}
echo "</tbody>";
echo "</table>";
?>
would like to integrate this to select statement:
<?php
$table_name2 = "person";
$column_name2 = "person_status";
echo "<select name=\"$column_name2\"><option>Select one</option>";
$sql1 = 'SHOW COLUMNS FROM '.$table_name2.' WHERE field="'.$column_name2.'"';
$row1 = $db_con->query($sql1)->fetch(PDO::FETCH_ASSOC);
$selected1 = '';
foreach(explode("','",substr($row1['Type'],6,-2)) as $option) {
if ($status == $option){
$selected1 = "selected=selected";
}else{
$selected1='';
}
echo "<option value='$option'" . $selected1. ">$option</option>";
}
echo "</select></br>";
?>
Get options only once (no need to repeat this for every person):
$sqlStatuses = 'SHOW COLUMNS FROM '.$table_name2.' WHERE field="'.$column_name2.'"';
$rowStatuses = $db_con->query($sql1)->fetch(PDO::FETCH_ASSOC);
$personStatuses = explode("','",substr($rowStatuses['Type'],6,-2));
Then, walk over the persons
foreach ($results2 as $value2) {
// Your code
echo "<tr>";
echo "<td>Name #".$s."<input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."' readonly=\"readonly\"/><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #".$s." First Name\" type=\"text\" value='" . $value2['first_name'] ."'/></td>";
// Added
echo '<td><select name="person_status_'.$s.'">';
foreach($personStatuses as $option) {
echo '<option value="'.htmlspecialchars($option).'" ';
if ($value2['person_status'] == $option) {
echo 'selected="selected"';
}
echo '>' . htmlspecialchars($option) . '</option>';
}
echo '</select></td>';
// Your code again
echo "</tr>";
$s++;
}
Building this into one SELECT-query is unneccessary complex (although possible, but gives you unreadable code).
Oh, and take a look at htmlspecialchars(), if a name contains a "-character your HTML get screwed up

PHP Insert Multidimensional Array into mysql

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

Categories