using sprintf

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

using sprintf

Post by nite4000 »

I am using some code I found online it works great and I have inserted all my db information for the table but its not inserting and I don't know what else to try. I tied to call the query and run error chk and all is find for errors and all but just wont insert into the db so I thought someone here would see something I am not seeing.

What this does.. is when a user chooses a date in the future it will insert that many records into the table for that many days.

Code: Select all

// sample data from user input:
$start_date =  date('Y-m-d');
$end_date = $_POST['end'];
$surftime = date('H:i:s');


// create values for each date:
$startTime = strtotime($start_date);
$endTime = strtotime($end_date);
$values = array();
for($time = $startTime; $time <= $endTime; $time = strtotime('+1 day', $time))
{
   $thisDate = date('Y-m-d', $time);
   $values[] = "(null,{$_SESSION['admin_id']}, $start_date, $surftime, 15,15, yes)";
}
// build the actual query:
$query = mysql_query(
"INSERT INTO surfstat (id,usrid, date, time, pgviews, num ,received_pay) VALUES\n%s", implode(",\n", $values));

any help on this issue would be great
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sprintf

Post by requinix »

You didn't quote the $surftime or "yes" values.

mysql_error
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: using sprintf

Post by nite4000 »

that didn't work.i having it down to the query itself isn't being executed somehow and I even tried mysql_query($query); but that didn't work either.

I tried " , ". , '., '". and none of them works
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: using sprintf

Post by priyankagound »

String values in MySQL queries must be enclosed in single quotes.

change your code as per the below example code:
$addDiary[] = '('.$current_user->id.', '.$_POST['exid_'.$i].', '.$_POST['qty_'.$i].', '.$calories_mod.", '".implode('.', $addTimestamp)."')";

I strongly suggest that you also pass every value through mysql_real_escape_string() before gluing it to sql query.

Hope this helps.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: using sprintf

Post by nite4000 »

believe it or not that still didn't help. it still don't insert into the table however it don't throw any errors either

Correction on the code this is the orginal code here

Code: Select all


// create values for each date:
$startTime = strtotime($start_date);
$endTime = strtotime($end_date);
$values = array();
for($time = $startTime; $time <= $endTime; $time = strtotime('+1 day', $time))
{
   $thisDate = date('Y-m-d', $time);
   $values[] = "('{$_SESSION['admin_id']}', '$thisDate', '$surftime', '15','15', 'yes')";
}

// build the actual query:
$query = sprintf(
   "INSERT INTO surfstat (usrid, date, time, pgviews, num ,received_pay) VALUES\n%s",
   implode(",\n", $values)
);

// show what query would look like:
echo "<pre>$query</pre>"; 

the echo shows it correct but its not going into the table.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sprintf

Post by requinix »

If it didn't insert then there was an error. Regardless of whether you actually check for it.
requinix wrote:mysql_error
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: using sprintf

Post by nite4000 »

tell me something I don't know.. I know there is a error/issue but im trying to figure out what that is
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using sprintf

Post by requinix »

requinix wrote:
requinix wrote:mysql_error
Post Reply