passing php variable in mysql update - php

I am trying to pass global variable in $_post in following way..
Unable to get through..Please help
<?php
include '/home/mechdipl/db.inc';
echo $_GET['lec'];
$lect=$_GET['lec'];
$atnds=$_GET['atnds'];
$size = count($_POST['id']);
$i = 0;
while ($i < $size) {
$name = $_POST['name'][$i];
$lectr = $_POST[$lect][$i];
$atndsr = $_POST[$atnds][$i];
$id = $_POST['id'][$i];
$query = "UPDATE stud SET name='$name', $lect ='$lectr',$atnds ='$atndsr' WHERE id = '$id' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$name<br /><br /><em>Presenty Updated!</em><br /><br />";
++$i;
}
?>

You have errors in your query, use this
UPDATE stud SET name='$name', lect ='$lectr', atnds ='$atndsr'
WHERE id = '$id' LIMIT 1
and you just wrote the query, not performing it, for that you have to use mysqli_query() or mysql_query() function....please let me know if you have any more doubts. and even in your $_POST global variable you have used
$lectr = $_POST[$lect][$i]; $atndsr = $_POST[$atnds][$i];
I know that it is not correct way. So, please before querying print out $_POST global variable and then use those keys in above. It could be like this
$lectr = $_POST['lect'][$i];
$atndsr = $_POST['atnds'][$i];

Related

Select query for getting a single value is not working

$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help

Updating value to database using PHP

I have some problem when want to update my value in database. There is no error shown. The page will ust redirect as indicated even when the value is not updated.
This is the code for user to input..
echo "<td bgcolor='#FFFFFF'><input id='id' name='pro_id[]' type='text'></td>";
echo "<td bgcolor='#FFFFFF'><input id='name' name='pro_name[]' type='text'></td>";
echo "<td bgcolor=‘#FFFFFF’><input id='quan' name='pro_quan[]' type='text'></td>";
Below is the code for my insert value..
$query = "INSERT INTO product (username, pro_id, pro_name, pro_quan) VALUES ";
for ($i = 0; $i < count($_POST['pro_id']); $i++) {
$query .= " ('{$username}', '{$id[$i]}', '{$name[$i]}', '{$quan[$i]}'),";
}
$query = substr($query, 0, -1);
$result = mysqli_query($con, $query) or die(mysqli_error($con));
The insert code work fine. The value is inserted into the database.Below is my update code..
$sql = "SELECT * FROM product where username = '$username'";
foreach($_SESSION['product'] as $item)
{
$id = $item['pro_id'];
$name = $item['pro_name'];
$quan = $item['pro_quan'];
$sold = $item['pro_sold'];
$sql="UPDATE product SET pro_id='".$id."', pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE username = '".$username."'";
}
$results=mysqli_query($con, $sql);
The value couldn't be updated. I have no idea what have gone wrong.So, any help will be appreciated.Thanks
$id and $quan in your update query are between single quotes. I don't know anything about your database structure, but something tells me those values are numbers and not strings. Here is the updated line:
$sql="UPDATE product SET pro_id=".$id.", pro_name='".$name."', pro_quan=".$quan.", pro_sold='".$sold."'";
You might have to remove the quotes around $sold as well.
Need to put sql execution in foreac, as below...
$sql = "SELECT * FROM product where username = '$username'";
$results=mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($results)){
$id = $row['pro_id'];
$name = $row['pro_name'];
$quan = $row['pro_quan'];
$sold = $row['pro_sold'];
$sql="UPDATE product SET pro_id='".$id."', pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE pro_id = '".$id."' ";
mysqli_query($con, $sql);
}
first of all check the pro_id that is primary key or not.
if it is a primary key than write query in this way.
$sql="UPDATE product SET pro_name='".$name."', pro_quan='".$quan."', pro_sold='".$sold."' WHERE pro_id = '".$id."' ";
because primary key generate error if the uniqueness of the column is disturb.
Thanks guys for all your helping. I have change my update code by using for loop.
Below is the solution..
for ($i = 0; $i < count($_POST['pro_id']); $i++) {
$sql="UPDATE product SET pro_name='".$name[$i]."', pro_quan=".$quan[$i].", pro_sold=".$sold[$i]." WHERE pro_id=".$id[$i]."";
$results=mysqli_query($con, $sql);
}

how to declare a php variable from a mysql fetch result?

i know it might be somthing old but i need to declare a variable or that is what i think.
basically i got a table name request and i need to fetch the last value from the column date. which i did in phpMyAdmin. now i need to fecth that exact value and use it as a variable in php.
<?php
include 'mydb.php';
require_once 'init.php';
$user_id = $user->data()->id ;
$data = mysql_query("SELECT date FROM request WHERE user_id= $user_id ORDER BY latest_request DESC LIMIT 1") or die(mysql_error());
$test = array();
while($row = mysql_fetch_array( $data ))
{
echo "<td>" .$row['date']. "</td>" ;
}
?>
<?php
$current_date = echo "<td> .$row['date']. "</td>" ;
// result im expecing is 2016-10-17 23:53:48
// echo " $current_date" --- will be 2016-10-17 23:53:48
>?
but all i get is an empty value, how can i accomplish this
If you want to have only one, last row, then maybe better idea would be something like that:
$sql = 'SELECT date FROM request WHERE user_id= $user_id ORDER BY latest_request DESC LIMIT 1';
$result = mysql_query($sql, $yourDBCon) or die(mysql_error());
$row = mysql_fetch_assoc($result);
print_r($row);
This solutions brings you only one row.
Also make sure you get anything from sql statment.
Here's a working code (based on the fact that you say that the while()-loop works, and that you actually get a result with the $row['date']:
<?php
include 'mydb.php';
require_once 'init.php';
$user_id = $user->data()->id ;
$data = mysql_query("SELECT date FROM request WHERE user_id = $user_id ORDER BY latest_request DESC LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array( $data )) {
echo $lastdate = '<td>'.$row['date'].'</td>' ;
}
echo $current_date = '<td>'.$lastdate.'</td>' ;
?>
i was able to solve this issues by using pdo insted
<?php
$base_datos = DB::getInstance();
$base_datos->query ("SELECT req_date FROM request wHERE user_id = $user_id order by req_date desc limit 1");
$last_date = $base_datos->results();
$request_last_date = $last_date[0];
$c_date =$request_last_date->req_date;
echo "$c_date";
?>
this was to get a value from a table on my database and declared it as a value

Update one value at multiple ID's in database table

$upload_files=implode(' ',$_GET['upload_files']);
$upload_user=",".$_GET['upload_user'];
echo $upload_files;
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id IN ('".$upload_files."')";
IN takes a comma-delimited string I believe.
try:
$upload_files=implode("','",$_GET['upload_files']);
Well, I got the solution. I used for loop to achieve the result.
$upload_files=$_GET['upload_files'];
$upload_user=",".$_GET['upload_user'];
for ($i = 0, $count = count($upload_files); $i <= $count; $i++) {
$sql = "UPDATE {$db_pr}files SET userID = CONCAT(userID,'".$upload_user."') WHERE id = '".$upload_files[$i]."'";
$result = mysqli_query($mysqli,$sql) or die("Error occurred - tried to update file.");
}
echo "<div class='loginMessage loginSuccess'>Assigned Successfully!!!</div>";

While loop not printing all answers

This might be really simple, but i cannot figure out the problem with this code:
$sql = mysql_query("select * from Temporary_Stock_Transfer where Emp_ID = '$emp_id' and Company_ID = '$company_id'");
if(mysql_num_rows($sql) == 0) {
echo "<tr><td colspan='3'><i>You currenty have no items</i></td></tr>";
}else {
while($row = mysql_fetch_array($sql)) {
echo mysql_num_rows($sql);
echo 'reached';
$book_id = $row[1];
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
echo "<tr><td>".$sql[0]."</td><td>".$row[2]."</td><td><span class='label label-important'>Remove</span></td></tr>";
}
}
Now based on my database the query is returning 2 results, the echo mysql_num_rows($sql) also gives out 2. However the reached is echoed only once. Does anyone see a potential problem with the code?
P.S: My bad, $sql is being repeated, that was a silly mistake
It will only echo once because of this line :
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
your overwriting the $sql variable.
Why not just run a single query and join the data you require ?
try ($sql2 instead of $sql and $sql[0] to $sql2[0]):
$sql = mysql_query("select * from Temporary_Stock_Transfer where Emp_ID = '$emp_id' and Company_ID = '$company_id'");
if(mysql_num_rows($sql) == 0) {
echo "<tr><td colspan='3'><i>You currenty have no items</i></td></tr>";
}else {
while($row = mysql_fetch_array($sql)) {
echo mysql_num_rows($sql);
echo 'reached';
$book_id = $row[1];
$sql2 = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
echo "<tr><td>".$sql2[0]."</td><td>".$row[2]."</td><td><span class='label label-important'>Remove</span></td></tr>";
}
}
I think it'll be because you are ressetting the sql variable
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
when you are still using it in the while loop. :P
It's probably this line:
$sql = mysql_fetch_row(mysql_query("select title from Book where Book_ID = '$book_id'"));
Try using a different variable name.
You are changing the $sql variable inside the loop, therefore the next time you run $row = mysql_fetch_array($sql), the value will be different (probably won't be any based on your code).
Inside the while loop you are re-using the same variable: $sql. This is used to control the terminating condition of the loop.
Using a different variable name, e.g. $sql2 should leave the original variable intact and the loop should proceed as expected.

Categories