Welcome to WebmasterWorld Guest from 23.20.193.33

Forum Moderators: open

Message Too Old, No Replies

Strange Update syntax error

wrong data is getting inserted to db

     
9:24 am on Sep 6, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2010
posts:3
votes: 0


Hey,

im using a php script to get info from an excel doc and import it into a database.
the problem im having is that when i run the script it only inserts 1s into the database for eg
if i was trying to insert 45 into db it would put a 1

heres the code im using i my self cant find anything wrong


require_once '../Excel/reader.php';



$data = new Spreadsheet_Excel_Reader();

$data->read("export2010-09-06 (1).xls");

//error_reporting(E_ALL ^ E_NOTICE);
$coloumn = $_POST['table'];
$file = $_POST['file'];

mysql_connect("127.0.0.1","user","pass");
mysql_select_db("jedi");
for ($j = 1; $j <= $data->sheets[0]['numRows']; $j++)
{

$name = $data->sheets[0]['cells'][$j+1][1];
$score = $data->sheets[0]['cells'][$j+1][4];
$output = explode("/",$score);

$query = "UPDATE main SET `". $coloumn ."`=" . $output[0] . ", `" . $coloumn . "t`=" . $output[1] . " WHERE `name`='" . $name . "'";
echo $query."<br>";
mysql_query($query) or die(mysql_error());
echo $data->sheets[0]['cells'][$j+1][1]."<br>";


}
i have the script echo the name and the syntax its using what it outputs is the following

UPDATE main SET `ohs`=16, `ohst`=16 WHERE `name`='Bob'
Bob

but all im getting in ohs is 1 and in ohst im getting nothing

thanks in advance guys
4:05 pm on Sept 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


What are the data types of those columns?
4:14 pm on Sept 6, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2010
posts:3
votes: 0


they are set too varchar
4:54 pm on Sept 8, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Then that's your problem. First, they really shouldn't be, if they are integers you want an integer field, it is faster and more appropriate. Then this would work.

UPDATE main SET `ohs`=16, `ohst`=16 WHERE `name`='Bob'

because you you don't need to quote integer fields. What I find surprising though is that this should give you a mysql syntax error and it doesn't, and is instead inserting a 1. Try quoting your fields, which is required on textual and date types.

UPDATE main SET `ohs`='16', `ohst`='16' WHERE `name`='Bob'
12:56 pm on Sept 9, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2010
posts:3
votes: 0


yer i tried setting them to int but it still does not work.

also tried quoting just gives sql error
5:53 pm on Sept 9, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


It shouldn't . . . . even though quoted aren't required on numeric fields it's still ok to quote them.


$query = "UPDATE main SET `". $coloumn ."`='" . $output[0] . "', `" . $coloumn . "t`='" . $output[1] . "' WHERE `name`='" . $name . "'";

or if these will always be integer fields,

$query = "alter table main change `" . $coloumn . "` `" $coloumn . "` int(11)";

$query = "alter table main change `" . $coloumn . "t` `" $coloumn . "t` int(11)";
6:18 pm on Sept 9, 2010 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5683
votes: 79


Maybe it's a PHP (or MySQL) thing, but unless your field names actually have backwards apostrophes in them, it seems like "UPDATE main SET `ohs`='16', `ohst`='16' WHERE `name`='Bob'" should throw an error. Methinks it should be "UPDATE main SET ohs='16', ohst='16' WHERE `name`='Bob'" (with no backwards apostrophes around the field names).
5:14 am on Sept 10, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


It is a mysql thing. The backticks are not usually necessary, but avoids any issues you have with naming a field that is a reserved word. Example, if you manage to name a column "date" or "curdate", those are a data types and an internal mysql function, respectively.

insert into table (`date`,`curdate`) values ('2010-01-01',curdate());
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members