mysql UPDATE not updating the mysql table row - php

This is my first time with UPDATE (mysql) this code isn't send the updates to my mysql database/table/row. I spent hours on php.net , but from what I can see at my current level or php knowledge its right and since I'm wrong, them I' dumb =}.
I turned loggin on mysql - nothing useful
This is from ZEND server: So it appears to be passing, but it is not updating in the mysql database.
Function Name: mysql_query
Function Arguments:
'UPDATE ads SET adcode = \'7000sbjhbjhbhjb\' WHERE ads_ID = 8'
any pointers/help is much appreciated.
<?php
require 'config.php';
// Report all Errors
//error_reporting(-1);
//ini_set('display_errors','On');
//connect to DB
mysql_connect("$host", "$db_user", "$db_password");
mysql_select_db("$db_name");
$query = "SELECT * FROM ads";
$result = mysql_query($query);
$num = mysql_numrows($result);
echo "<h1><center>AD's Currently Available</center></h1><br /><br />";
$i=0;
while ($i < $num) {
$ID = mysql_result($result,$i,"ID");
$adname = mysql_result($result,$i,"adname");
$currentadcode = mysql_result($result,$i,"adcode");
// Delete AD item by ID number
$action = (isset($_REQUEST['action']));
if ($_GET['action'] == "deletead") { // remove AD
mysql_query("DELETE FROM ads where ID = '$_GET[IDnum]'");
$i=$i++;
header("Location: " . $_SERVER['PHP_SELF']);
}
$letknown = "<b>AD removed</b><br />";
**// Edit AD code
if (isset($_POST['editad' . $ID])) {
$newadcode = mysql_real_escape_string($_POST['adcode' . $ID]);
$doedit = "UPDATE ads SET adcode = '" . $newadcode . "' WHERE ads_ID = " . $ID;
$updatead = mysql_query($doedit);
header("Location: " . $_SERVER['PHP_SELF']);**
}
$letknown = "<b>Ad Edited</b><br />";
echo "<b>$ID : $adname</b><br />Delete Ad<br /><br />\n";
echo "Preview :<br /><div class=\"adcode\">$currentadcode</div><br /> \n";
echo "<br />\n";
echo "<form action=\"displayads.php\" name=\"addAD$ID\" method=\"post\">\n";
echo "AD code (can be any type of script) text link, javascript or banner :<br />\n";
echo "Code :<br /><textarea name=\"adcode$ID\" wrap=\"physical\" cols=\"60\" rows=\"5\" onKeyDown=\"textCounter(document.addAD$ID.adcode$ID,document.addAD$ID.remLen$ID,5000)\" onKeyUp=\"textCounter(document.addAD$ID.adcode$ID,document.addAD$ID.remLen$ID,5000)\">$currentadcode</textarea>";
echo "<br /><input readonly type=\"text\" name=\"remLen$ID\" size=\"3\" maxlength=\"3\" value=\"5000\">Characters Left \n";
echo "**<input type=\"submit\" name=\"editad$ID\" value=\"Edit AD Code\">**</form>\n";
echo "<br /><hr />";
$i++;
}
?>

You are missing closing double quotes :
$doedit = "UPDATE ads SET adcode = '" . $newadcode . "' WHERE ads_ID = '". $ID."'";
Or simply write your query like this :
$doedit = "UPDATE ads SET adcode = '$newadcode' WHERE ads_ID = '$ID'";
Use LIMIT 1 if you want to update only a single row

Related

PHP problem with insert into database in while loop

i try to write a website for library as an exercise. I have while loop to display all books in my database. If user is logged in and state(stan) of book(ksiazka) is free(Wolny) it shows button under book. After clicking it takes all free books to database and update their state as hired not only that one which user want. Here is the code, thanks.
$findbook2 ="select ksiazka.id_ksiazka, ksiazka.tytul, ksiazka.id_stan, autor.id_autor, autor.imie_autor, ksiazka.rok_wydania, autor.nazwisko_autor, stan.id_stan, stan.nazwa_stan FROM ((ksiazka inner join autor ON ksiazka.id_autor = autor.id_autor) inner join stan ON ksiazka.id_stan = stan.id_stan);";
$stan = mysqli_query($connect, $findbook2);
while($row = mysqli_fetch_array($stan))
{
echo "Tytuł:" . " " .$row['tytul']." ". "Autor:" . " " . $row['imie_autor']." ". $row['nazwisko_autor']. " ". "Rok wydania" . " ". $row['rok_wydania'] . " ". "Stan ". $row['nazwa_stan']. " ";
if(isset($_SESSION['id_czytelnik'])){
if($row['nazwa_stan']=='Wolny'){
echo '<form method = "GET" action = "ksiazki.php">';
echo '<input type = "submit" name = "submit" value = "Wypożycz"/>';
echo '</form>';
if(isset($_REQUEST['submit'])){
$id_czytelnik = $_SESSION['id_czytelnik'];
$id_ksiazka = $row['id_ksiazka'];
$data_oddania = date('Y-m-d', strtotime('+30 days'));
$wstaw_ksiazke = "INSERT INTO `wypozyczenie`(`id_wypozyczenie`, `id_czytelnik`, `id_ksiazka`, `id_pracownik`, `data_wypozyczenia`, `data_oddania`) VALUES ('','$id_czytelnik','$id_ksiazka',2,NOW(),'$data_oddania')";
if(mysqli_query($connect, $wstaw_ksiazke)){
$update = "Update ksiazka set id_stan = 3 where id_ksiazka = '$id_ksiazka'";
if(mysqli_query($connect, $update)){
echo "Wypozyczyłeś książkę";
}
}
}
}
}
echo "</br>";
}
Consider the statement if(isset($_REQUEST['submit'])) which is always true inside your while loop after clicking submit as it is not unset anyway.It causes repeated execution of a statement while the Array is not empty.

Display data from database depending on the how many I have entered

In my database and more specific in the table named portfolio a user with a speficic username can store more than one data. The columns of the database are id,username,portfolio and portfolio_description
and I want to echo in my web page all the data for this username for example username=nbourlai. Until now I can echo only the first row for each username what is the loop that i can use in order to echo as many time is the id number.
Below is the current result and i want to create Portfolio and Portfolio description to be displayed with the appropriate values as many time as the highest id number is for the specific user.
Here is my code...
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
$row = mysql_fetch_row($portfolio);
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
Can you help me please?
Are you looking for while loop like this?
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
echo "<h2>Portfolio</h2>";
while($row = mysql_fetch_row($portfolio)){
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
}
or, like this?
$portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio ORDER BY username");
$username = '';
while($row = mysql_fetch_row($portfolio)){
if($username != $row[0]){
echo "<h1>".$row[0]."</h1>";
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}else{
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}
$username = $row[0];
}

PHP pagination add ID

I have a kind of problem, with following php code:
$host="localhost";
$user_name="";
$pwd="";
$database_name="";
$conexiune = mysql_connect($host,$user_name,$pwd) or die("Nu ma pot conecta la MySQL!");
mysql_select_db($database_name, $conexiune) or die("Nu gasesc baza de date");
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM citate ORDER BY id DESC LIMIT $start_from, 1";
$rs_result = mysql_query ($sql,$conexiune);
while ($row = mysql_fetch_assoc($rs_result))
echo "<img src='" . $row['poza'] . "' />
<br />
" . $row['titlu'] . "
<br />
" . $row['descriere'] . "
<br />
" . $row['data'] . "
";
$sql = "SELECT COUNT(id) FROM citate";
$rs_result = mysql_query($sql,$conexiune);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
$pagelink ='<< ';
$pagelink_2='>> ';
if($page>1)
echo $pagelink;
if($page<2)
echo "";
for ($i=1; $i<=$total_pages; $i++) {
if ($i != $page)
echo "<a href='lista.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
if ($i==$page)
echo " <strong>". $i . "</strong> "; // defining class in the style sheet you can add colour or border to the displayed number
};
if($page<$total_pages)
echo $pagelink_2;
that code offer me pagination (u allready know that) , and the url bar adress look's like following:
http://www.site.ro/folder/lista.php?page=PAGE-NUMBER
i want to look like following:
http://www.site.ro/folder/lista.php?citat=SOME-NUMBERS&page=PAGE-NUMBER
my database table its populated like that:
--------------------------------------------------------------
| id | poza | titlu | descriere | citat | data | accesari |
--------------------------------------------------------------
i want to extract data from "citat" column , so link from url bar will look like:
http://www.site.ro/folder/lista.php?citat=EXTRACTED-FROM-CITAT&page=PAGE-NUMBER
every time when i press on next page buton, will look like:
http://www.site.ro/folder/lista.php?citat=2748925&page=1
http://www.site.ro/folder/lista.php?citat=2840194&page=2
etcetera..
how can i modify that code?
Thank in advance !
I am ignoring all your security issues.
This will work as long you display only one item per page:
$last_citat = 0;
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<img src='" . $row['poza'] . "' /><br />" . $row['titlu'] . "<br />" . $row['descriere'] . " <br />" . $row['data'] . "";
$last_citat = $row['citat'];
}
and later:
$pagelink ='<< ';
$pagelink_2='>> ';
if($page>1) { echo $pagelink; }
if($page<2) { echo ""; }
for ($i=1; $i<=$total_pages; $i++) {
if ($i != $page) {
echo "<a href='lista.php?citat=".$last_citat."&page=".$i."'>".$i."</a> ";
}
if ($i==$page) {
echo " <strong>". $i . "</strong> ";
}
}
Do you want to filter database results by 'citat' value?
If so, then you have to build links with get parameter 'citat' in them, like:
<<
and later take GET['citat'] and add it in the sql query where part so that only results with certain citat value would be returned, like:
$sql = "SELECT * FROM citate WHERE citat = '".GET['citat']."' ORDER BY id DESC LIMIT $start_from, 1";
NOTE: This is not real example and it is WRONG TO USE LIKE WRITTEN: you must escape GET['citat'] because otherwise your database will be hacked very easily and very soon!
Get value of citat from while loop:
$citat = $row['citat'];
Then you can use $citat wherever and however you want, and to check the citat parameter in url you can do it by:
if (isset($_GET['citat'])) {
//it's there do something
} else {
//it's absent
}

PHP Form data not stored when submitted?

I am working on an synonym/alias manager database where the user has the ability to store an association they feel is right. Say the user types in "rabbit" and no synonyms or aliases are found. The user then decides to associate "rabbit" with "bunny" and stores that into a database. Anytime any other user types in "bunny" the results for "rabbit" will appear. However, I am trying to implement a polling system asking the user if they feel the association is correct. If they think "bunny" fits "rabbit" then they vote yes, otherwise no. This is where I am stuck. As soon as I load the poll and press submit everything disappears and nothing gets sent to the database. Code is below:
$query = "SELECT * from searchtestdb where engname in ( SELECT synonyms.synonym FROM words LEFT JOIN synonyms ON synonyms.word_id = words.word_id WHERE word LIKE '%$searchBox%') "; // Query for animals in db
$query = mysql_query($query);
if(mysql_num_rows($query) == 0)
{
echo "<h2>Aliases: </h2>";
echo "Sorry, but we can not find an alias to match your query.";
echo "<br> ";
}
else
{
echo "<h2> Results using Alias: </h2>";
while($result = mysql_fetch_array($query))
{
$query2 = "SELECT * from searchtestdb where engname LIKE '%".$result['engname']."%';";
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2))
{
print "<h4>Latin Name: </h4> ";
echo $row["latname"];
echo "<br> ";
print"<h4>English Name:</h4> ";
echo $row["engname"];
echo "<br>";
print "<h4> Species: </h4> ";
echo $row["spectype"];
print "<h4>Characteristics: </h4> ";
echo $row["charc1"];
echo "<br>";
echo $row["charc2"];
echo "<br>";
echo $row["charc3"];
echo "<br>";
}
}
$self = $_SERVER['PHP_SELF'];
print "<form method='post' action='$self' >\n";
print "<h4>Alias Association Correct? : </h4>";
print "<p>" .
"<input type='radio' name='vote' id='vote' value='1' /> \n" .
"Yes" .
"<input type='radio' name='vote' id='vote' value='2' /> \n" .
"No" .
"</p> \n" .
"<p>" .
"<input type='submit'name='submitVote' value='Submit' />" .
"\n </p> \n" .
"</form> \n" .
$vote=htmlentities($_POST['vote']);
echo $vote;
mysql_connect(----------------------) or die(mysql_error());
mysql_select_db("-----------") or die(mysql_error());
if($vote == 1)
{
mysql_query("INSERT INTO items(yes, uNo, word_id) VALUES ('0', '0', bunny');");
mysql_query("UPDATE items SET yes=yes+1 WHERE word_id='bunny';");
echo 'Thanks for voting Yes!';
}
if($vote == 2)
{ mysql_query("INSERT INTO items(word_id) VALUES ('".$result['engname']."'') ");
mysql_query("UPDATE items SET uNo=Uno+1 WHERE word_id='".$result['engname']."'");
echo "changos";
}
}
In a nutshell I made a small mistake:
$self = $_SERVER['PHP_SELF'];
should be
$self = $_SERVER['POST'];
Thanks Anyway.

extract array values from a function

Below is a function in PHP I created which returns some product information in an array. On the web page where I am calling this function I want to be able to specify exact array elements for example just the product description ($result2['itm_desc']). Please can someone point me in the right direction as to do this. I would assume you call a function like fetch array but i'm not quite sure how to execute this.
public function getAllReelImages(){
$sql = "SELECT id FROM $this->table3 WHERE itm_cat = 2 ORDER BY id ASC";
echo "<br /><br />";
$stmt = mysqli_query($this->connection, $sql);
/*fetch values*/
while($result = mysqli_fetch_array($stmt)){
echo "<br /><br />";
$sql2 = "SELECT itm_details.id,itm_details.itm_make,itm_details.itm_model,itm_details.itm_desc,itm_pic_detail.itm_pic_name, itm_value.itm_sale_price
FROM
itm_details, itm_pic_detail, itm_value
WHERE
itm_details.id = {$result['id']} AND itm_pic_detail.id = {$result['id']}
AND itm_value.id = {$result['id']} ORDER BY id ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
echo ($result2['id']); echo"<br />";
echo ($result2['itm_make']); echo"<br />";
echo ($result2['itm_model']); echo"<br />";
echo ($result2['itm_desc']); echo"<br />";
echo ($result2['itm_sale_price']); echo"<br />";
echo "<img src='$this->dir"."{$result2['itm_pic_name']}'><br />";
echo"<br />";
echo $value;
}
}
}
First of all.. Your code is pretty bad. I made it little bit more clear. For example: echo ($result2['id']); echo"<br />"; TO-> echo $result2['id'] . '<br />'; (for future references.)
They way your code is at the moment. You wont add anything to an array, since you echo them right away. I made my version, that would first create the array and then later you can use that array..in any way you want :)
And doesn't the ORDER BY use by default as ASC?
I also changed the first query more dynamic.
NOTE: I'm not very good with mysql table joining, somebody must validate the second query. But in my eyes, its absolutely incorrect. However, I'm posting this in the reference for creating an array with a function and then displaying it.
NOTE2: I hope your question was about PHP o.0
# This should be in some class ?!
function getAllReelImages ($category) {
$sql = "SELECT * FROM `" . $this->table3 . "` WHERE `itm_cat` = '" . $category . "' ORDER BY `id` ASC";
$stmt = mysqli_query($this->connection, $sql);
while($result = mysqli_fetch_array($stmt)) {
$sql2 = "SELECT `itm_details.id`, `itm_details.itm_make`, `itm_details.itm_model`, `itm_details.itm_desc`, `itm_pic_detail.itm_pic_name`, `itm_value.itm_sale_price` FROM `itm_details`, `itm_pic_detail`, `itm_value` WHERE `itm_details.id` = '" . $result['id'] . "' AND `itm_pic_detail.id` = '" . $result['id'] . "' AND `itm_value.id` = '" . $result['id'] . "' ORDER BY `id` ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
$returns[$result2['id']]['id'] = $result2['id'];
$returns[$result2['id']]['itm_make'] = $result2['itm_make'];
$returns[$result2['id']]['itm_model'] = $result2['itm_model'];
$returns[$result2['id']]['itm_desc'] = $result2['itm_desc'];
$returns[$result2['id']]['itm_sale_price'] = $result2['itm_sale_price'];
$returns[$result2['id']]['itm_pic_name'] = $this->dir . $result2['itm_pic_name'];
}
}
return $returns;
}
# Display the results:
echo '<br /><br />';
foreach (getAllReelImages(2) as $img_id => $img_value) {
echo $img_id . '<br>';
echo $img_value['itm_make'] . '<br>';
echo $img_value['itm_model'] . '<br>';
echo $img_value['itm_desc'] . '<br>';
echo $img_value['itm_sale_price'] . '<br>';
echo $img_value['itm_pic_name'] . '<br>';
echo '<hr>';
}
echo '<br />';

Categories