Update in fetch array - php

Is it possible to execute an UPDATE in a mysql_fetch_array() loop? Like this:
$query = "SELECT * FROM inbox";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result) or die(mysql_error())){
echo "<div>";
echo "<form method='POST'>";
echo "<h1>".$row['link']."</h1>";
echo "<h3>".$row['tittle']."</h3> na";
echo "<input type='text' name='tittle'>";
echo "<h3>".$row['content']."</h3>";
echo "<textarea name='content'></textarea>";
echo "<input type='submit' name='".$row['link']."' value='Change'>";
echo "</form>";
echo "</div>";
$tit = $_POST['tittle'];
$ten = $_POST['content'];
$link = $row['link'];
if (isset($_POST[$link])) { mysql_query("UPDATE inbox SET tittle='$tit' content='$ten' WHERE link='$link'");}
}
It have to do update in mysql_fetch_array(), because I wanna to change content of that things.

You have an error in your syntax as the values should be , delimited:
if (isset($_POST[$link])) {
mysql_query("
UPDATE inbox SET tittle='$tit',
content='$ten'
WHERE link='$link'
");}
Note:
You should take a look at the mysqli class to handle your future queries. It's just as simple as your current method, but more secure and not deprecated.

Related

PDO update not returning variables

I'm trying to use PDO to update a row in my postgres database.
The form is not sending the variables to the handler file.
I'm not sure where the problem lies, I've been battling with this for a few days.
Form
//$maxContent is set and available
//$context_number is set and available
echo"<form method='post' action='updateSpatialPhoto_handler.php'>";
$query3 = $conn->prepare("SELECT * FROM excavation.contexts_spatial_photographs
WHERE contexts_spatial.area_easting = {$_SESSION['area_easting']}
AND contexts_spatial.area_northing = {$_SESSION['area_northing']}
AND contexts_spatial.context_number = {$_SESSION['context_number']}");
//contexts_spatial_photographs
$query3->execute();
while($r = $query3->fetch(PDO::FETCH_OBJ))
{
// for each needed
echo"<input type='hidden' name='photograph_date' value='".$r->photograph_date."'>";
echo"<input type='hidden' name='photograph_number' value='".$r->photograph_number."'>";
echo"<input type='hidden' name='primary_shot' value='".$r->primary_shot."'>";
echo"<input type='hidden' name='maxContext' value='", $maxContext,"'>";
};
echo"<input type='submit' value='Update Spatial Photo'>";
echo "</form>";
handler
<?php
session_start();
//
include 'connect/connect.php';
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!isset($_SESSION['photograph_date'])) {$_SESSION['photograph_date'] = $_POST['photograph_date'];}
if (!isset($_SESSION['photograph_number'])) {$_SESSION['photograph_number'] = $_POST['photograph_number'];}
if (!isset($_SESSION['primary_shot'])) {$_SESSION['primary_shot'] = $_POST['primary_shot'];}
if (!isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
if (isset($_SESSION['photograph_date'])) {$_SESSION['photograph_date'] = $_POST['photograph_date'];}
if (isset($_SESSION['photograph_number'])) {$_SESSION['photograph_number'] = $_POST['photograph_number'];}
if (isset($_SESSION['primary_shot'])) {$_SESSION['primary_shot'] = $_POST['primary_shot'];}
if (isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
//echo "Photograph Date: "; echo $_SESSION['photograph_date']; echo "<br />";
//echo "Photograph Number: "; echo $_SESSION['photograph_number']; echo "<br />";
//echo "Primary Shot: "; echo $_SESSION['primary_shot']; echo "<br />";
try {
$sql3 = "UPDATE excavation.contexts_spatial_photographs SET
context_number = :context_number
WHERE contexts_spatial_photographs.area_easting = $area_easting
AND contexts_spatial_photographs.area_northing = $area_northing
AND contexts_spatial_photographs.context_number = $context_number";
$stmt2 = $conn->prepare($sql3);
// prepare sql and bind parameters
$stmt2->bindParam(':context_number', $maxContext, PDO::PARAM_INT);
$stmt2->execute();
echo "Record updated successfully in contexts spatial photographs<br />";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
?>
At the top of the handler, your using lines like...
if (isset($_SESSION['maxContext'])) {$_SESSION['maxContext'] = $_POST['maxContext'];}
These are setting the values in the session variables, which is fine.
But in your SQL -
AND contexts_spatial_photographs.context_number = $maxContext";
$maxContext doesn't seem to be set anywhere. This may be the same as the session variables, so you need
$maxContext = $_SESSION['maxContext'];
or
AND contexts_spatial_photographs.context_number = $_SESSION['maxContext']";
Although it would be even better if you use bindParam with them, the same way you use it with :context_number.

update sql after form submit

Okay guys, I'm newbie here but
1. I want to UPDATE my sql after I input number on the text box. it echos correctly but doesn't change the DB.
2. I want to remove the first submit button and make it onchange event.
I'm really confused here. Thanks guys! This work is almost done. been doing it for a long time already. I'm still learning php, please bear wit me. thanks
<?php
$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$result = $conn->query("select * from english");
echo "<html>";
echo "<body>";
echo "<form name='form' method = POST>";
echo "<select name = 'Students'>";
while ($row = $result->fetch_assoc()) {
$LRN = $row['LRN'];
$Last = $row['Last_Name'];
$First = $row['First_Name'];
echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
}
echo "</select>";
echo "<input type='submit' name='submit' value='Show'>";
if (isset($_POST['Students'])) {
$lrn = $_POST['Students'];
$stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
$stmt->bind_param('i', $lrn);
$stmt->execute();
$stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
$stmt->fetch();
echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table>";
}
///////////EDIT DATA
echo "Edit Data: ";
echo "<select name = 'Edit'>";
echo '<option value=Q1>Q1</option>';
echo '<option value=Q2>Q2</option>';
echo '<option value=Q3>Q3</option>';
echo '<option value=Q4>Q4</option>';
echo '<option value=FINAL>FINAL</option>';
echo '<input type="number" name="editdata">';
echo "</select>";
echo "<input type='submit' name='submit2' value='Edit Now'>";
if (isset($_POST['Edit'])) {
$upd = $_POST['Edit'];
$txt = $_POST['editdata'];
$now = "UPDATE english SET $upd=$txt WHERE LRN=$lrn";
$result2 = $conn->query($now);
echo $now;
}
echo "</form>";
echo "</body>";
echo "</html>";
?>
You need to wrap your input in quotes or else SQL thinks you're trying to reference columns instead of strings.
$now = "UPDATE english SET $upd=\"$txt\" WHERE LRN=\"$lrn\"";
or if you prefer single quotes to avoid escaping w backslash then this should work the same:
$now = "UPDATE english SET $upd='$txt' WHERE LRN='$lrn'";
Also, this is not best practice, as mentioned by chris85 you are subject to injection attacks, if you want best practice then you want to use this: http://php.net/manual/en/mysqli-stmt.bind-param.php

Create hyperlink on table result and fill editable form in other page

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>";
}
?>

Get name on the select option value but get wrong id.

$sql = "SELECT distinct s.doc_id, s.pat_id as pat_id, p.pat_fullname as fullname, p.pat_id from patient p, patientscript s WHERE s.doc_id = '$doc_id' AND s.status = '1' AND s.pat_id = p.pat_id;";
$result = mysql_query($sql) OR
die("Database Error. MYSQL-Error:".mysql_error()."\n");
echo "<form name='form'> ";
echo "<label>Patient :</label>";
echo "<select name='patname'>";
echo "<option>Select a patient</option>";
while ($row = mysql_fetch_array($result))
{
$patname = $row['fullname'];
$pat_id = $row['pat_id'];
echo "<option value='$patname'>$patname</option>";
}
echo "</select>";
echo "<input type='button' value='Submit' onClick='get();' >";
echo "<input type='hidden' name='pat_id' value='$pat_id'/>";
echo "</form>";
echo "<div id='showName'></div>";
Let say, there are 2 echo results from option value, A and B.
When select A, I get 12(id) A from output
When select B, I get 12(id) B from output
Actually the 12 is for B, A is 7, anyone can help me solve this problem.
Try this:
(...)
echo "<select name='patdata'>";
echo "<option>Select a patient</option>";
while ($row = mysql_fetch_array($result))
{
$patname = $row['fullname'];
$pat_id = $row['pat_id'];
echo "<option value='".$patname."-".$pat_id."'>$patname</option>";
}
echo "</select>";
echo "<input type='button' value='Submit' onClick='get();' >";
echo "</form>";
(...)
Then, (I'm assuming you are fetching the results after post)
$patdata = explode( '-', $_REQUEST['patdata']);
$patname = $patdata[0];
$pat_id = $patdata[1];

PHP if else to check ipaddress against ones stored in table

I've got just a quick little poll and a query to check and see if the IPaddress is already in the table to know if someone has already voted. I have the table created already and it works.
My question is with the if else statement it is not working. I am trying to make it
if ip address is in the table show string
else show poll
$ip=$_SERVER['REMOTE_ADDR'];
include("../db/config.php");
include("../db/opendb.php");
$result = mysql_query("SELECT * FROM Poll WHERE ip='$ip'");
if ($result==$ip) {
echo "Thank you for voting.";
} else {
echo "<form action=logvote.php method=post>" .
"<input type=radio name=ans value=ans1> Answer1<br>" .
"<input type=radio name=ans value=ans2> Answer2<br>" .
"<input type=radio name=ans value=ans3> Answer3<br>" .
"<input type=radio name=ans value=ans4> Answer4<br>" .
"<input type=submit value=Submit>";
echo "<input type=hidden name=ip value=";
echo "$ip>";
echo "</form>";
include("../db/closedb.php");
}
Thanks in advance.
You simply forgot to fetch a row first. Also, you can get the necessary information with a simple count:
$result = mysql_query("SELECT COUNT(*) as 'count' FROM Poll WHERE ip='$ip'");
$row = mysql_fetch_assoc($result);
if ($row['count']) {
// show string
} else {
// show poll
}
Your code should be:
$query = mysql_query("SELECT ip FROM Poll WHERE ip='$ip'");
$info = mysql_fetch_object($query);
$result = $info->ip;
if($result == $ip)
mysql_query() returns a reference not a string.
Also, using ip in the SELECT statement will speed up your code.
mysql_query does not return the data directly but rather a reference to the result.
You can get the number of rows found for the query with:
$num = mysql_numrows($result);
So you can check by:
$result = mysql_query("SELECT ip FROM Poll WHERE ip='$ip'");
if (mysql_numrows($result)) {
// dostuff
} else {
// do other stuff
}
// you can retrieve the ip like so:
$data = mysql_fetch_assoc($result);
$ip = $data['ip'];

Categories