i have index.php and index2.php
here is index.php:
when i submit this value, index2.php will show. Here is index2.php:
I am not understanding, why this error msg is showing.
Anyways, submitting these selected values(index2.php) it will say a disease name from the database. clicking on submit, this text is showing(below):
I am pesting my code here:
index.php
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("symptomchecker",$con) or die(mysql_error());
$q = "select dtid,diseasetype from disease_type";
$result = mysql_query($q,$con) or die(mysql_error());
$numRB = mysql_num_rows($result);
echo "<html><head><title>Disease Type</title></head><body><h1>Disease List</h1></br><form action='index2.php' method='post'>";
$i=0;
while($row = mysql_fetch_array($result)){
echo $row['diseasetype'];
echo "<input type='radio' name='diseasetype' id='pet1' value='$row[dtid]' />" ;
echo "<br />";
$i++;
}
echo " <input type='submit' value='submit' name='submit'></form></body></html>";
?>
index2.php:
$diseasetypeid = "$_POST[diseasetype]";
$j=0;$i=0;
$query = "select did from disty_dis_rel where dtid = ".$diseasetypeid."";
$result1 = mysql_query($query,$con) or die(mysql_error());
echo "<html><head><title>symptom</title></head><body><h1>symptom List</h1></br><form action='' method='post'>";
while($row = mysql_fetch_array($result1))
{
$z=$row['did'];
$query2 = "select sid,symptomname from symptom where sid = ".$z."";
$result2 = mysql_query($query2,$con) or die(mysql_error());
while($row2 = mysql_fetch_array($result2))
{
echo $row2['symptomname'];
echo "<input type='checkbox' name='pets[$i]' id='pet1' value='$row[sid]' />" ;
echo "<br />";
$i++;
}
$j++;
}
echo " <input type='submit' value='submit' name='submit'></form></body></html>";
if(isset($_REQUEST[submit]))
{
**foreach ($_REQUEST[pets] as $key=>$values)**
{
$yy.=$values."-";
}
$yy=rtrim($yy,"-");
$xx = "select did,sids from dis_sym_rel where sids=".$yy."";
$result3 = mysql_query($xx,$con) or die(mysql_error());
while($row = mysql_fetch_array($result3)){
$p=$row[did];
$xxx = "select did,diseasename from disease where did=".$p."";
$result4 = mysql_query($xxx,$con) or die(mysql_error());
while($row4 = mysql_fetch_array($result4))
{
echo $row4[diseasename];
}
}
}
?>
the line between ###doublestar### is the error line(49).
Just within index2.php :
$diseasetypeid = "$_POST[diseasetype]";
should be
$diseasetypeid = $_POST["diseasetype"];
and
if(isset($_REQUEST[submit]))
should really be this :
if($_SERVER['REQUEST_METHOD'] == 'POST')
this checks the REQUEST_METHOD rather than a form value. And
foreach ($_REQUEST[pets] as $key=>$values)
should be
foreach ($_REQUEST['pets'] as $key=>$values)
and I suggest you read about sql injection
This code:
foreach ($_REQUEST[pets] as $key=>$values)
Should be changed to:
foreach ($_REQUEST['pets'] as $key=>$values)
You should quote (put apostrophe or double-quotes, before and after your array key if it's string).
Then, you must have a form something to the following (i can't find it in your index.php file).
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
The array key 'pets' is the name of your input. [] indicates array. In foreach ($something as ...), $something must be an array (or object).
Related
I'm trying to update my data in MySQL database using the displayed data from it. But I haven't much luck in figuring out what I'm doing wrong. I don't know how can I pass my data from my $rows in order for me to use it in my UPDATE SQL statement.
I just want to complete all the data before I clicked the save button.
Please see attached image for the output..
And also, I got a warning message like this.
WHenever I Clicked the Save Data button..
Notice: Undefined variable: reason in C:\xampp\htdocs\BTR\get_forms.php on line 109
Notice: Array to string conversion in C:\xampp\htdocs\BTR\get_forms.php on line 109
Can someone enlighten me.. How to do this.? Thank you..
<?php
require 'config.php';
$form_type = $_POST['form_type'];
if ($form_type == 'MCCV-F2'){
$region = $_POST['region'];
$province = $_POST['province'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barangay'];
$period = $_POST['period'];
$form_type = $_POST['form_type'];
echo "NON COMPLIANT IN EDUCATION<br>";
echo "<br><br>MUNICIPALITY: ".$municipality;
echo "<br><br>BARANGAY: ".$barangay;
echo "<br><br>PERIOD: ".$period;
?>
<form name="get_forms_f2" action="" method="post">
<br><br>
<center><table border = 1 style =2 width=1800>
<tr>
<td><center><b>Household ID </center></td>
<td><center><b>Member ID </center></td>
<td><center><b>Name</center></td>
<td><center><b>Sex</center></td>
<td><center><b>HH Status</center></td>
<td><center><b>Grade Level </center></td>
<td><center><b>School ID</center></td>
<td><center><b>Name Of Dominant School</center></td>
<td><center><b>CV Remarks</center></td>
<td><center><b>Reason</center></td>
<td><center><b>Other Reason</center></td>
<td><center><b>Intervention</center></td>
</tr>
<?php
$sql = "SELECT A.family_id, A.barangay, A.person_id, A.gender, A.family_status, A.current_grade_level,
A.school_facility_id, A.school_facility_name, A.municipality, CONCAT(B.last_name, ', ',B.first_name) as 'name',
B.person_id,B.cv_remarks, B.reason, B.other_reason, B.intervention, B.status FROM roster AS A RIGHT JOIN compliance AS B ON A.person_id = B.person_id
WHERE B.period='$period' AND B.form_type='$form_type' AND A.municipality='$municipality' AND A.barangay='$barangay'";
$query=$conn->prepare($sql);
$query->execute();
$result= $query->fetchALL(PDO::FETCH_ASSOC);
$count=(int)$query->rowCount();
foreach ($result as $row){
$person_id[] = $row['person_id'];
echo "<tr>";
echo "<td>".$row['family_id']."</td>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['family_status']."</td>";
echo "<td>".$row['current_grade_level']."</td>";
echo "<td>".$row['school_facility_id']."</td>";
echo "<td>".$row['school_facility_name']."</td>";
echo "<td><input type='text' name='cv_remarks[]' value='".$row['cv_remarks']."'></td>";
echo "<td><select name='reason[]'>";
if (is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value=''>SELECT REASON FOR Non-Compliance</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
if (!is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value='".$row['reason']."'>".$row['reason']." (SELECTED)"."</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
echo "</select></td>";
echo "<td><input type='text' name='other_reason[]' value='".$row['other_reason']."'></td>";
echo "<td><input type='text' name='intervention[]' value='".$row['intervention']."'></td>";
echo "</tr>";
}
}
?>
</table></center><br><br>
<input type="submit" name="submit" value="Save Data">
<?php
$sql3 = "UPDATE compliance SET reason='{$reason}' WHERE person_id='{$person_id}' AND form_type='$form_type' AND period='$period'";
$query = $conn->prepare($sql3);
$query->execute();
?>
</form>
I have posted with the answer of how to update form with an array variable as a whole.
I have kept person,reason as array since you have added that alone as array().
<?php
require 'config.php';
if(isset($_POST['submit']))
{
for ($i=0; $i<count($_POST['person_id']); $i++)
{
$sql3 = "UPDATE compliance SET reason='".$_POST['reason'][$i]."' WHERE person_id='".$_POST['person_id'][$i]."' AND form_type='".$_POST['form_type']."' AND period='".$_POST['period']."'";
$query = $conn->prepare($sql3);
$query->execute();
}// end of For
}// end of IF
$form_type = $_POST['form_type'];
if ($form_type == 'MCCV-F2'){
$region = $_POST['region'];
$province = $_POST['province'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barangay'];
$period = $_POST['period'];
echo "NON COMPLIANT IN EDUCATION<br>";
echo "<br><br>MUNICIPALITY: ".$municipality;
echo "<br><br>BARANGAY: ".$barangay;
echo "<br><br>PERIOD: ".$period;
?>
<form name="get_forms_f2" action="" method="post">
<br><br>
<center><table border = 1 style =2 width=1800>
<tr>
<td><center><b>Household ID </center></td>
<td><center><b>Member ID </center></td>
<td><center><b>Name</center></td>
<td><center><b>Sex</center></td>
<td><center><b>HH Status</center></td>
<td><center><b>Grade Level </center></td>
<td><center><b>School ID</center></td>
<td><center><b>Name Of Dominant School</center></td>
<td><center><b>CV Remarks</center></td>
<td><center><b>Reason</center></td>
<td><center><b>Other Reason</center></td>
<td><center><b>Intervention</center></td>
</tr>
<?php
$sql = "SELECT A.family_id, A.barangay, A.person_id, A.gender, A.family_status, A.current_grade_level,
A.school_facility_id, A.school_facility_name, A.municipality, CONCAT(B.last_name, ', ',B.first_name) as 'name',
B.person_id,B.cv_remarks, B.reason, B.other_reason, B.intervention, B.status FROM roster AS A RIGHT JOIN compliance AS B ON A.person_id = B.person_id
WHERE B.period='$period' AND B.form_type='$form_type' AND A.municipality='$municipality' AND A.barangay='$barangay'";
$query=$conn->prepare($sql);
$query->execute();
$result= $query->fetchALL(PDO::FETCH_ASSOC);
$count=(int)$query->rowCount();
foreach ($result as $row){
?>
<input type="hidden" name="person_id[]" value="<?php echo $row['person_id'];?>">
<input type="hidden" name="form_type" value="<?php echo $form_type; ?>">
<input type="hidden" name="period" value="<?php echo $period; ?>">
<?php
echo "<tr>";
echo "<td>".$row['family_id']."</td>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['family_status']."</td>";
echo "<td>".$row['current_grade_level']."</td>";
echo "<td>".$row['school_facility_id']."</td>";
echo "<td>".$row['school_facility_name']."</td>";
echo "<td><input type='text' name='cv_remarks[]' value='".$row['cv_remarks']."'></td>";
echo "<td><select name='reason[]'>";
if (is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value=''>SELECT REASON FOR Non-Compliance</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
if (!is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value='".$row['reason']."'>".$row['reason']." (SELECTED)"."</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
echo "</select></td>";
echo "<td><input type='text' name='other_reason' value='".$row['other_reason']."'></td>";
echo "<td><input type='text' name='intervention' value='".$row['intervention']."'></td>";
echo "</tr>";
}
}// end of MCCV-F2 form
?>
</table>
</center>
<br><br>
<input type="submit" name="submit" value="Save Data">
</form>
Hope so this would solve your problem br. have a try and let me know if any hindrance you find.
Trying to make a CRUD, everything works except my Update function. I feel like the problem is in the second sql query. When I click on submit it just refreshes and the change is gone. Can anyone show me how to find what I need to change/show me what to change?
<head>
<title>Update</title>
</head>
<body>
</form>
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form action="" method="post">';
echo "Company: <input type=\"text\" name=\"CName\" value=\"".$row['CName']."\"></input>";
echo "<br>";
echo "Contact: <input type=\"text\" name=\"Contact\" value=\"".$row['Contact']."\"></input>";
echo "<br>";
echo "City: <input type=\"text\" name=\"City\" value=\"".$row['City']."\"></input>";
echo "<br>";
echo "<input type=\"Submit\" = \"Submit\" type = \"Submit\" id = \"Submit\" value = \"Submit\">";
echo "</form>";
}
echo "</table>";
} else {
echo "0 results";
}
if(isset($_POST['Submit'])){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
?>
Instead of building a form inside PHP, just break with ending PHP tag inside your while loop and write your HTML in a clean way then start PHP again. So you don't make any mistake.
Also you've to submit your $id from your form too.
Try this
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?= $id ?>" />
Company: <input type="text" name="CName" value="<?= $row['CName'] ?>" />
<br>
Contact: <input type="text" name="Contact" value="<?= $row['Contact'] ?>" />
<br>
City: <input type="text" name="City" value="<?= $row['City'] ?>" />
<br>
<input type="Submit" name="Submit" id="Submit" value="Submit" />
</form>
<?php
} // end while loop
echo "</table>";
}
else {
echo "0 results";
}
Note: You are passing undefined variables into your update query. As you are submitting your form you must have to define those variables before you use them.
if (isset($_POST['Submit'])) {
$CName = $_POST['CName'];
$Contact = $_POST['Contact'];
$City = $_POST['City'];
$id = $_POST['id'];
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
that loop? ID primary key or not?
maybe u need create more key in table dealer like as_id
<input type="hidden" name="idform" value="$as_id">
in statment
if($_POST){
$idf = $_POST['idform'];
if(!empty($idf)){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where as_id=$idf";
$result = $conn->query($sql);
}
$conn->close();
}
I have some checkbox options that I save in the DB. I was able to view and also select multiple options and save them in the DB. The issue is that I want to display the saved information but I don't know how to do that.
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
Save into DB
<?php
session_start();
$id = $_SESSION['user_id'];
//$id = 3;
include ('mysql_connect.php');
$insStr = '';
foreach($_POST['comp'] as $val){ $insStr .=$val.","; }
mysql_query("INSERT INTO competency_result (user_id,result) VALUES ( '$id', '$insStr' )") or die(mysql_error());
echo'<script>alert("Inserted Successfully")</script>';
?>
All I want to do now is to display the saved information in a table format. I tried doing this but it only showed me the saved ID
<?php
$res= mysql_query("SELECT * FROM competency_result WHERE user_id = '$user'")or die(mysql_error());
while($row = mysql_fetch_array($res))
{
echo"<tr>";
echo"<td> $row[result]</td>";
?>
<?php
echo"</tr>";
}
?>
<form action="save_comp.php" method="post">
<?php
//Display
include ('mysql_connect.php');
$sql = mysql_query("SELECT * FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
echo"<input type='checkbox' name='comp[". $row['id']. "]' value='". $row['competency'] ."' /> ".$row['competency']." <br />";
}
?>
<input name="submit" type="submit" value="submit" />
</form>
If you want to checkboxes check then you can try with below code:
<?php
$sql = mysql_query("SELECT name FROM competency ");
//$row = mysql_fetch_array($sql);
while($row = mysql_fetch_array($sql))
{
$focus=explode(",",$row['name']);
?>
<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Comp",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> >
<?php
}
?>
I can't seem to be able to update any records except the first one.
I am not sure how to modify any of the displayed records.
<?php
if(isset($_POST["action"]) == "update")
{
$id = $_POST['m_id'][0];
$type = $_POST['type'][0];
// if I echo $id & $type, it only gives me the first record.**
mysql_query("
UPDATE membership_type
SET mt_type ='$type'
WHERE mt_id = '$id'"
);
}
?>
ALl of this is within the same php page.
<form name=form action='' method='post'>
<?php
$result=mysql_query("SELECT * FROM membership_type;");
while($rows=mysql_fetch_array($result))
{ ?>
<input size=35 class=textField type=text name='type[]' value='<?php echo $rows['mt_type']; ?>'>
<input type=hidden name='m_id[]' value="<?php echo $rows['mt_id']; ?>">
<input type=submit value="Update">
<?php
}
?>
How do I edit any of the displayed records by simply clicking Update button???
First: You should NEVER use the mysql_* functions as they are deprecated.
Second: Try this code:
<?php
// Get a connection to the database
$mysqli = new mysqli('host', 'user', 'password', 'database');
// Check if there's POST request in this file
if($_POST){
foreach($_POST['m_id'] as $id => $type){
$query = "UPDATE membership_type
SET mt_type = '".$type."'
WHERE mt_id = '".$id."'";
// Try to exec the query
$mysqli->query($query) or die($mysqli->error);
}
}else{
// Get all membership_type records and then iterate
$result = $mysqli->query("SELECT * FROM membership_type") or die($mysqli->error); ?>
<form name='form' action='<?php echo $_SERVER['PHP_SELF'] ?>' method='post'>
<?php while($row = $result->fetch_object()){ ?>
<input size='35'
class='textField'
type='text'
name='m_id[<?php echo $row->mt_id ?>]'
value='<?php echo $row->mt_type; ?>'>
<input type='submit' value="Update">
<?php } ?>
</form>
<?php } ?>
Third: In order to add more security (this code is vulnerable), try mysqli_prepare
Only the first record is updated on every form submission because you have set $id = $_POST['m_id'][0], which contains the value of the first type[] textbox. To update all the other records as well, loop through $_POST['m_id'].
Replace it. Hope this works.
<?php
if(isset($_POST["action"]) == "update")
{
$id = $_POST['m_id'];
$type = $_POST['type'];
$i = 0;
foreach($id as $mid) {
mysql_query("UPDATE membership_type
SET mt_type='".mysql_real_escape_string($type[$i])."'
WHERE mt_id = '".intval($mid)."'") OR mysql_error();
$i++;
}
}
?>
Try this :
if(isset($_POST["action"]) == "update")
{
$id = $_POST['m_id'];
$type = $_POST['type'];
$loopcount = count($id);
for($i=0; $i<$loopcount; $i++)
{
mysql_query("
UPDATE membership_type
SET mt_type ='$type[$i]'
WHERE mt_id = '$id[$i]'"
);
}
}
You HTML was malformed and you were passing as an array but then only using the first element. Consider:
<form name="form" action="" method="post">
<?php
$result = mysql_query("SELECT * FROM membership_type;");
while($row = mysql_fetch_array($result))
echo sprintf('<input size="35" class="textField" type="text" name="m_ids[%s]" value="%s" />', $row['mt_id'], $row['mt_type']);
?>
<input type="submit" value="Update">
</form>
Then the server script:
<?php
if(isset($_POST["action"]) && $_POST["action"] == "Update"){
foreach($_POST['m_ids'] as $mt_id => $mt_type)
mysql_query(sprintf("UPDATE membership_type SET mt_type ='%s' WHERE mt_id = %s LIMIT 1", addslashes($mt_type), (int) $mt_id));
}
There are other things you could be doing here, eg. prepared statements, but this should work.
I am trying to create a compare option between selected cars.
<?php
if(isset($_POST['compares'])) {
$id_nums = array($_POST['cBox']);
//$id_nums = array(1,6,12,18,24);
$id_nums1 = implode(", ", $id_nums);
$query = "SELECT * FROM wp_cars WHERE id in ($id_nums1)";
$cCars = mysql_query($query) or mysql_error();
while($car = mysql_fetch_array($cCars)) {
echo $car['cartitle']."<br/>";
echo $car['saleprice']."<br/>";
}
} else {
$query1 = "SELECT * FROM wp_cars";
$allcars = mysql_query($query1) or die(mysql_error()); `
while($car1 = mysql_fetch_array($allcars)) {
echo "<input type='checkbox' value=".$car1['id']." name='cBox[]' />";
echo $car1['cartitle']."<br/>";
echo $car1['saleprice']."<br/>";
}
}
?>
How to pass the checkbox name(cBox[]) array based on checkboxes selection.
<form action="compares.php" method="post">
<button name="compares">Select Cars to Compare</button>
</form>
$id_nums = array($_POST['cBox']);
$_POST['cBox'] is already an array, you are making a 2d array. Doing
$id_nums1 = implode(", ", $_POST['cBox']);
would do what you want. Although it is wide open to SQL injection.
From your HTML part, send inputs this way:
<form action="compares.php" method="post">
<?php foreach (mysql_fetch_array($allcars) as $car: ?>
<input type="checkbox" value="<?php echo $car['id']; ?>" name="cBox[]" />
<?php echo $car['cartitle']; ?><br />
<?php echo $car['saleprice']; ?><br />
<?php endforeach; ?>
</form>
And in your PHP part, receive inputs this way:
$id_nums = implode(",", $_POST['cBox']);