Mysql query Update not add values - php

I want to update mysql records there are code above. But i take error. What can i do
This is my listed mysql info
sayfa.php
while ($veri = mysql_fetch_array($sorgu1)) {
echo "<tr>
<td>{$veri['id']}</td>
<td>{$veri['isim']}</td>
<td>{$veri['durum']}</td>
<td>{$veri['sira']}</td>
<td>{$veri['seo_baslik']}</td>
<td>{$veri['seo_aciklama']}</td>
<th>[SİL] |
DÜZENLE</th>
</tr>";
}
i want to get id's values from sayfa.php
duzenle.php
<?php
$baglan = mysql_connect('localhost','root','12345') or die ('mysql sunucuya bağlanamadı.');
$veritabani_sec = mysql_select_db('vtben', $baglan);
mysql_query("SET NAMES 'utf8'");
$_POST=array_map('mysql_real_escape_string',$_POST);
if(is_numeric($_GET[id]) && $_GET[duzenle]==1){
$sorgu = mysql_query("SELECT * FROM sayfalar WHERE id='$_GET[id]'");
$kayit = mysql_fetch_assoc($sorgu);
print_r($kayit);
echo <<<HTML
<h3>Sayfa Güncelleme</h3>
<form action="duzenle-guncelle.php" method="post">
<input type="hidden" name="id" value="{$kayit[id]}">
<p><label for="isim">Başlık</label>
<input type="text" name="isim" value="{$kayit[isim]}"></p>
<p><label for="icerik">İçerik</label>
<textarea name="icerik" id="summernote"> {$kayit[icerik]}</textarea></p>
<p><label for="sira">Menü Sırası</label>
<input type="text" name="sira" value="{$kayit[sira]}"></p>
<p><label for="durum">Durumunu giriniz. Eğer 0 ise ust menude gözükür 1 ise üst menüde yer alamaz. Sadece rakam olarak 0 veya 1 değeri giriniz</label>
<input type="text" name="durum" value="{$kayit[durum]}"> </p>
<p><label for="seo_baslik">SEO Başlığı</label>
<input type="text" name="seo_baslik" value="{$kayit[seo_baslik]}"></p>
<p><label for="seo_aciklama">SEO Açıklama</label>
<input type="text" name="seo_aciklama" value="{$kayit[seo_aciklama]}"></p>
<br />
<p><input type="submit" value="Güncelle"></p>
</form>
HTML;
}
mysql_close($baglan);
?>
and then, i want to change values but not working
duzenle.guncelle.php
<?php
require_once('dbbaglan.php');
$id=$_POST["id"];
$isim=$_POST["isim"];
$icerik=$_POST["icerik"];
$sira=$_POST["sira"];
$durum=$_POST["durum"];
$seo_baslik=$_POST["seo_baslik"];
$seo_aciklama=$_POST["seo_aciklama"];
$guncelle=mysql_query("update yazilar SET isim='$isim',icerik='$icerik',
sira='$sira',durum='$durum',seo_baslik='$seo_baslik',seo_aciklama='$seo_aciklama' where id='$id'");
if($guncelle) {
echo "guncelleme basarili oldu<br />
<a href='yazilar.php'>geriye donerek islemlere devam edebilirsiniz</a>";
}else{
echo "guncelleme basarisiz oldu";
}
?>

Related

How to display information of a selected user from a database for editing

I Am trying to edit user details from a database. I have queried the database and stored the info in $row, but each time I try to echo the details I get the following error: Trying to access array offset on the value of type null in
C:\xampp\htdocs\merchant\admin\edituserhis.php
I seem not to know what I am doing wrong in the SQL statement, I have checked the Variable $id and its working well.
<div class="quotes">
<?php
$con = mysqli_connect("localhost","root","","merchant_db");
$id = $_REQUEST['username'];
$query = "SELECT * from user_trans where userid='".$id."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
<?php echo $row['userid']; ?>;
<form name="form" method="post" action="userprofile.php">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['userid'];?>" />
<p><input type="text" name="balance" placeholder="Enter Amount" required value="<?php echo $row['description'];?>" /></p>
<p><input name="submit" type="submit" value="Credit" /></p>
</form>
</div>

mysqli_query of update statement isn't working in PHP

I have written code with an update statement but it doesn't affect the database. I have tried with and without single quotes but it still doesn't update the database. I have by default value of Details="Order Placed" in the table but when I update the value with the update query it doesn't work.
<?php
include("connection1.php")
?>
<form method="POST">
Enter Orderid to be updated<br>
<input type="text" name="id">
<br>
<input type="radio" id="m" name="info" value="placed">
<label for="m">Order Placed</label><br>
<input type="radio" id="f" name="info" value="shipped">
<label for="f">Order Shipped</label><br>
<input type="radio" id="o" name="info" value="Delivered">
<label for="o">Order Delivered</label>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$i=$_POST['id'];
$info1=$_POST['info'];
$query=("SELECT * FROM shipments WHERE OrderId='$i'");
$data=mysqli_query($conn, $query);
$total=mysqli_num_rows($data);
if($total==1)
{
$query1=mysqli_query("UPDATE `shipments` SET `Details`='$info1
WHERE`OrderId`='$i'");
if($result==mysqli_query($conn, $query1))
{
echo "ORDER UPDATED";
}
else
{
echo "ORDER NOT UPDATED";
}
}
}
?>
Just remove unwanted mysqli_query in Update statement $query1...
Change
$query1=mysqli_query("UPDATE `shipments` SET `Details`='$info1
WHERE`OrderId`='$i'");
To
$query1= "UPDATE shipments SET Details ='$info1' WHERE OrderId='$i'";

Radio button and search text box in php

I have my input form:
<form style="font-size: 10pt; font-family: 'Courier New'; color:black;font-weight:600;margin-left:15px;line-height:25px" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="radio" id="a" name="search" value="EntryId" checked>EntryId <font color="blue">(get Vocab information)</font>
<br />
<input type="radio" id="b" name="search" value="EntryId1">EntryId <font color="blue">(get disease name, protein name)</font>
<br />
<input type="radio" id="c" name="search" value="UniprotId">UniprotId <font color="blue">(get Gene name, Dna Seq)</font>
<br />
<input type="radio" id="d" name="search" value="Genename">Genename <font color="blue">(get Gene information)</font>
<br />
<input type="radio" id="e" name="search" value="EntryId3">EntryId <font color="blue">(get HTML, PubMed information)</font>
<br /><br /><br />
<input style="height:30px; width: 300px; border:black 1px solid" type="text" name="Search" >
<input style="height:30px" type="submit" value="Go">
</form>
In this form I am using six radio buttons and each one is representing a stored procedure which is stored in MySQL. If I select a radio button and enter a value in text box (which will be the parameter for my selected procedure) I want output for that procedure.
PHP code:
<?php
$id='';
$query='';
if (isset($_POST['Search']))
{
$id=$_POST['Search'];
}
echo "<table>";
//connect to database
$connection = mysqli_connect("localhost", "root", "", "mohammad_prostatecancer");
//run the store proc
if (isset($_POST['EntryId1']))
{
$query= "call DiseaseVocab(".'"'.$id.'")';
}
if (isset($_POST['EntryId2']))
{
$query= "call DiseaseProtein(".'"'.$id.'")';
}
if (isset($_POST['UniprotId']))
{
$query= "call getGene(".'"'.$id.'")';
}
if (isset($_POST['Genename']))
{
$query= "call Getname(".'"'.$id.'")';
}
if (isset($_POST['EntryId3']))
{
$query= "call Getdata(".'"'.$id.'")';
}
$result = mysqli_query($connection, $query) or die("Query fail: " . mysqli_error());
//loop the result set
echo "<tbody>";
// point to the beginning of the array
$check = mysqli_data_seek($result, 0);
?>
<tr>
<td><b>EntryId</b></td>
<td><b>Vocabid</b></td>
<td><b>VocabSourceName</b></td>
</tr>
<?php
while ($rownew = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach($rownew as $k => $v)
{
echo "<td>".$v."</td>";
}
echo "</tr>"."<br>";
}
echo "</tbody></table>";
?>
I am not getting any result. I get these warnings:
Warning: mysqli_query(): Empty query
Warning: mysqli_error() expects exactly 1 parameter, 0 given
You need the connection in the error, do it like this and then you can see the error.
mysqli_error($connection)
query is empty because you are checking
`if (isset($_POST['EntryId1']))` instead of `if (isset($_POST['search']) && $_POST['search']=='EntryId1')`
and switch case would be b`enter code here`etter in this scenario.

Update multiple SQL records using one PHP/MySQLi query

Is it possible to update multiple records in one MySQLi query?
There are 4 records to be updated (1 for each element level) when the submit button is clicked.
The results are posted to a separate PHP page which runs the query and returns the user back to the edit page. elementid is 1,2,3,4 and corresponds with Earth, wind, fire, water. These never change (hence readonly or hidden)
<form id="edituser" name="edituser" method="post" action="applylevelchanges.php">
<fieldset>
<legend>Edit Element Level</legend>
<?php
while($userdetails->fetch())
{?>
<input name="clientid" id="clientid" type="text" size="8" value="<?php echo $clientid; ?>" hidden />
<input name="elementid" id="elementid" type="text" size="8" value="<?php echo $elementid;?>" hidden />
<input name="elemname" id="elemname" type="text" size="15" value="<?php echo $elemname; ?>" readonly />
<input name="elemlevel" id="elemlevel" type="text" size="8" required value="<?php echo $elemlevel; ?>" /></br>
</br>
<?php }?>
</fieldset>
<button type="submit">Edit Student Levels</button>
</form>
And the code to apply the changes
<?php
if (isset($_POST['clientid']) && isset($_POST['elementid']) && isset($_POST['elemname']) && isset($_POST['elemlevel'])) {
$db = createConnection();
$clientid = $_POST['clientid'];
$elementid = $_POST['elementid'];
$elemname = $_POST['elemname'];
$elemlevel = $_POST['elemlevel'];
$updatesql = "update stuelement set elemlevel=? where clientid=? and elementid=?";
$doupdate = $db->prepare($updatesql);
$doupdate->bind_param("iii", $elemlevel, $clientid, $elementid);
$doupdate->execute();
$doupdate->close();
$db->close();
header("location: edituserlevel.php");
exit;
} else {
echo "<p>Some parameters are missing, cannot update database</p>";
}

trying to edit SQL via php but keep getting an error

as far as i can see my code is sound however, I keep getting an error
this is the error
Notice: Undefined variable: person in
\sql\modify.php on line 12
here is my code..
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
if(isset($_POST['sumbmit'])) {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modify";
header("Location: index.php");
}
?>
any Thoughts or am im I just blind???
<?php
include 'includes/connection.php';
// set $person veriable
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
// if form submit you use update and redirect
else {
$u = "UPDATE people SET `Name` = '$_POST[inputName]', `Description` = '$_POST[inputDesc]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
//echo "User has been modify"; // this not need, bcz execute header('location') redirect you current page
header("Location: index.php");
exit(); //use it after header location
}
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
You just need to check if you actually got output.
Just a plain example:
if ($person):
?>
<h1>You are modifying A User</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify"/>
</form>
<?php
endif;
Remember to always validate your input and output, but also if queries you try to run do produce a result set.
Here's the issue:
<?php
include 'includes/connection.php';
if (!isset($_POST['submit'])){
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
}
?>
At this point $person is set only if the form hasn't been submitted; but later on:
Name<input type="text" name="inputName" value="<?php echo $person['Name']; ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $person['Description']; ?>" />
You're using it anyway. If the form has been submitted, then you're going to get the warning you're seeing. What you need to do is something like:
if (isset($_POST['submit'])){
$name = $_POST['name'];
$description = $_POST['description']
} else {
$q = "SELECT * FROM people WHERE ID = $_GET[id]";
$result = mysql_query($q);
$person = mysql_fetch_array($result);
$name = $person['name'];
$description= $person['description'];
}
And then:
Name<input type="text" name="inputName" value="<?php echo $name ?>" /><br />
Description<input type="text" name="inputDesc" value="<?php echo $description; ?>" />
The variables are now set either way.
A couple of other things - you're not doing any error checking to see if your query has worked; if the query fails, your code will carry on regardless.
Secondly, the mysql_ functions are deprecated and will stop working at some point; you should look at moving to using mysqli_* or PDO instead.

Categories