MySQL won't update if content is changed - php

I got a PHP and MySQL problem, when I update the headline I got no problem. But when updating while having changed the content it won't update at all.
Any idea to what's going wrong.
$query = mysql_query("UPDATE about SET headline='$headline' WHERE content='$content'")
or die(mysql_error());
$query = mysql_query("SELECT * FROM about ORDER BY id DESC");
while ($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$headline = $row['headline'];
$content = $row['content'];
header("location: ../");
}
I'm coding in PHP and using and MySQL server.
I would prefer a solution that only calls the database once when updating.

When using an UPDATE statement, the WHERE clause is used to specify the record(s) to update. In the following case, where you've modified both the headline and content:
UPDATE about SET headline='$headline' WHERE content='$content'
The above query attempts to update the headline field where the content field equals the new content. There won't be any rows where the existing content matches the new content.
You'd have to do something similar to:
UPDATE about SET headline='$headline', content='$new_content' WHERE content='$old_content'
However, you should generally be selecting a record by an identifier, not a content field.
UPDATE about SET headline='$headline', content='$new_content' WHERE id = $id
If you update using a field like content, you may accidentally update all rows that have the same content, instead of just the row that you're trying to update. Using a unique identifier allows you to update only the specified row.
You could store the identifier in the session (on the server side).

Related

Selecting a specific column in a row associated with a specific id

I am running into a little trouble here, I did some searches on Google and Stackexchange to see if someone else had answered this before but it didn't turn up useful content so I am asking for some assistance. I have a column name that does exist in the database however, for some reason the query to MySQL keeps saying that the index by that column name does not exist (Undefined index:) is what I am getting but it is defines in the table so I thought maybe I could go about this another way by requesting the id first, then fetch associated column_name value with that specific id row. Then I want to return that to the variable $column_name to make use of the variable in my next query. Normally when I do $id = $_REQUEST['id']; I get the current id for the item being rendered in the browser. But this "column_name" keeps returning an undefined index error. Yet it exist in the DB just as the ID column does. Why can I call on one and not the other?
How can I call on the specified column_name associated with the id in $_REQUEST['id']; global variable?
In the DB you find:
id = "Value"
itemId = "Item ID"
CategoryName = "Category"
If I request ID 1, how can I fetch the associated CategoryName with that ID?
Thanks for your help!
$id = $_REQUEST['id'];
if(!empty($id)){
$column_name = $this->dbConnection->query("SELECT column_name FROM table //That is directly associated with $id");
}
Add the $id in a WHERE clause in the query:
$column_name = $this->dbConnection->query("SELECT CategoryName FROM table WHERE id = $id");
Also, remember to escape all parameters you send to the databse, without it $id would be vulnerable to SQL Injection attacks.
Good luck :)
I was able to fix this thanks to #frz3993 by adding categoryName={some cat name} to the dynamically generated link redirecting to the page in question while also making the category value dynamically generated as well.
The previous page needed a modification to the link such as
href="DynamicallyGeneratedLink.php?categoryName=%s&id=%s"
This was able to make the category name available for the $_REQUEST[]; variable to then use it to create a new dynamically generated link that would allow the visitors to go back to the previous full list of items in the category without having to use the back button.
Now the following code looks like
function backToCategoryList($dbConnection){
$categoryName = $_REQUEST['categoryName'];
if($r = $this->dbConnection->query("SELECT categoryName FROM table WHERE categoryName = '$categoryName' LIMIT 1")){
while($d = $r->fetch_assoc()){
printf("<i class=\"icon icon-th\"></i>", $d['link'], $d['categoryName']);
}
}
}

php-mysql update without deleting data already there

I want to update a Comment Blob in a mysql database without deleting data already in it.
$SQLstring="INSERT INTO Participant(Role,Comment)
VALUES('$Costume','$Comment')
WHERE Fname='$FirstName' AND Lname='$LastName'";
This doesn't want to work if there is already data in the "Comment" field and if I use UPDATE it will replace the data.
I want to keep data that is there and simple add to it
You can use this to do it:
$Rolequery = mysql_query("UPDATE `Participant` SET Role=CONCAT(Role, ',$role') WHERE Fname='$FirstName' AND Lname='$LastName'");
$Commentquery = mysql_query("UPDATE `Participant` SET Comment=CONCAT(Comment, ',$Comment') WHERE Fname='$FirstName' AND Lname='$LastName'");

Primary Key of the DB as PHP header info

I have created a registration form which is in application.php and when submitted it is processed and the contents are stored using submit.php and I want to redirect to thanks.php with a parameter called message with value equal to the primay key of the recently inserted row.. For example it should return my 100th applicant to the page,
www.mydomain.com/thanks.php?message=100
where 100 is the Primary key (AI).
In other words I want to print the primary key of the table as a refernce id for the users.. I tried
header("location: thanks.php?message=".$id); and
header("location: thanks.php?message=".$_POST['id']);
Both dont work for me.
Kindly help me guys!
EDIT:
require("admin/sources/connection.php");
$id = mysql_insert_id();
// Other variables are declared here
$sql = "my_INSERT_query_goes_here";
$result = $conn->query($sql);
if($result) {
header("location: thanks.php?message=".$id);
}
This is my submit.php code
Wrong execution order - simple fix
You are first assigning $id, and then running the query.
Twist them around to make it work.
Not using auto-increment?
I see in the query in your comment, that you manually pass the id you want to enter. Did you realize that mysql_insert_id() will not return the sql column that you did call "id", but the column that you set an auto-increment on? That is a single row in your table that automaticly gets an incrementing number without the need to manually enter it.
Couple issues ... it looks like you're mixing mysql_ methods with mysqli and you have the execution in the wrong order.
If your database table is auto-increment, you don't need to pass an ID in the query. By calling $id = mysql_insert_id(); and using the id in the query, you're passing in 0 as the id which uses the next auto-increment id.
Getting the insert_id after $conn->query($sql); will return to you the insert id used.
Echo some variables to see what values you're getting for mysql_insert_id(); before and after.
However, if you're doing a $conn->query(), then you're not using the standard mysql_ functions.
Try this:
require "admin/sources/connection.php";
// Other variables are declared here
$sql = "my_INSERT_query_goes_here";
$result = $conn->query($sql);
if($result) {
$id = $conn->insert_id;
header("location: thanks.php?message=".$id);
}

Check for duplicate when editing a record in MySQL

I'm having problems with a piece of code and wonder if someone can help.
I have a form that submits information to a MySQL database, I have the correct code for checking to see if the submitted product code already exists, and if so shows a warning message and the record is not added.
That code is:
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code'");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - <b>$product_name</b> has <b>NOT</b> been added because the reference number already exists.");
}
That works fine for Data Entry, however I have another form that allows users to edit the record, this is what is causing me a problem, as the above code only tells me that there is already a matching record in the database, Of course when I try to save (update) the record it now tells me I can't because it is a duplicate.
What I would like to happen is that it doesn't allow users to choose another productcode that already exists, but I want them to be able to update the record using the same product code the form fetched from the database.
Hope that makes sense, any help greatly appreciated.
If you have id (primary key) then You will have to compare with id of that product before updating the record. For example
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code' AND id!=$id");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
echo "duplicate record";
}
Here $id is the id of the product that you should have while editing the record.
following is the step you need to follow when you managing the Database
First you need an primary key(auto_increment) in "ID" field
When you execute insert query that time first check where record is already available or not. if not available than only you should execute insert query.
use primary key filed for update, delete etc...
if you follow the above step than you never face this problem
Are you perhaps checking for duplicate occurrences in both of insert and update statement? If so, you shouldn't. Duplicate entry is relevant only when "inserting". You shouldn't use the same check for update. Hope that helps.
why the same code for update also? you can use another query for updating which is better for debugging if you have problems later. try this
$result = mysql_query("UPDATE listing_1 SET product_code='$new_product_code' WHERE product_code='$product_code' AND id='$id'");
if($result) {
echo "your product was updated.";
} else {
echo "your product is not in DB";
}
EDIT: be careful in updating or inserting things, take always id to check unless your product_code is unique
EDIT
I have resolved this issue by making $productcode field a unique Index.
Now when editing if there is a duplicate..
Mysql does not accept the update query, it returns an error code
I trap that error code and include it in an if statement...
if( mysql_errno() == '1062' )
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - $product_name has NOT been added because the reference number already exists");
}
adminmessage("Item Updated", Congratulations you updated $productname succesfully");
}
This now allows editing of $productcode but does not allow it to be changed to one already used in the database.
Thank you to everyone who took the time to offer help

MySQL (and maybe PHP): Fill a new column with the host of a referer

I have a table with a request log. In there is a column with referers. Now in order to see what domains refer the most requests I want to add a new column with the host of the referer.
That is no problem for the new entries but how do I update every row without an entry in the new row to have the host from the referer?
Is there an easy way to do this in MySQL or how di I solve it in PHP?
$oldRows = mysql_query("SELECT * FROM logs_requests WHERE host_ref = ''");
while($row = mysql_Fetch_array($oldRows))
{
$host = parse_url($row['url_ref'], PHP_URL_HOST);
mysql_query("UPDATE logs_requests SET host_ref='$host' WHERE id='{$row['id']}'");
}
This php code fetch all the rows with empty host_ref field,
then it takes the host (www.domain.com) from the url_ref (www.domain.com/page/34.html) with the parse_url function.
After that , there's an update query which updates the host field.
NOTICE: Customize this code according to your db scheme.

Categories