LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2008, 01:30 PM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
php mass update database field value


here is the php script i am working on

Code:
<?php
$mysqli = new mysqli("localhost", "root", "****" "htf");

if (mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}
else {
	/*$sql = "DELETE FROM ".$_GET['type']." WHERE id=".$_GET["id"];
	mysqli_query($mysqli, $sql);
	echo	"File Removed<br>"; */
for ($x = $_GET['id']; $x <= $_GET['highest']-1; $x++) {
$y=$x+1;
$sql = "UPDATE ".$_GET['type']." SET id=".$x." WHERE id = ".$y;
echo $sql."<br>";
	mysqli_query($mysqli, $sql) or die('failed');
}
mysqli_close($mysqli);
}
//echo "<meta http-equiv=\"refresh\" content=\"0; URL=\"./manage.php\">";
the idea is to delete a record from a database and decrement the remaining id numbers by 1 to fill the gap, but the sql query keeps dieng, any suggestions?
(yes the delete part is commented out for the time being until i can get the update part running)

Last edited by frieza; 04-01-2008 at 02:03 PM.
 
Old 04-01-2008, 05:42 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,444

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Well, I'd check that your vars are what you think e.g. echo out the SQL just before using it. Also, check the exact error from PHP eg
printf("Error: %s\n", mysqli_error($link));
as per
http://au.php.net/manual/en/function.mysqli-query.php
 
Old 04-02-2008, 03:18 PM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
You could find some more info from the error msgs.

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

Gentoo

Last edited by prad77; 04-17-2008 at 03:37 AM.
 
Old 04-02-2008, 05:50 PM   #4
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
The problem isn't immediately obvious to me. You should post the actual SQL you're executing and crank up error_reporting and show us what messages you're getting.

Also, you really don't need that for loop. You can do the whole update in one query:
Code:
UPDATE yourtable SET id = id - 1 WHERE id BETWEEN :deleted_id + 1 AND :highest - 1
The placeholders :deleted_id and :highest are your $_GET['id'] and $_GET['highest'] respectively. Also, since you're already using mysqli, you might want to look into prepared statements rather than using string concatenation to put data into your queries.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mass editing mysql tables via php. apepost Programming 1 12-17-2005 06:25 PM
Do PHP can make a user base update web site whitout Database? explorer1979 Programming 3 01-05-2005 09:52 AM
write an update query in shell prompt to update the database in sqlserver suchi_s Programming 2 09-29-2004 07:27 AM
php mass mail mus3ab Linux - Security 2 06-07-2004 01:38 AM
Mysql/PHP query problem with datetime field. Pcghost Programming 2 11-11-2003 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration