Page 1 of 1

using sprintf

Posted: Fri Oct 18, 2013 8:14 am
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

Re: using sprintf

Posted: Fri Oct 18, 2013 1:24 pm
by requinix
You didn't quote the $surftime or "yes" values.

mysql_error

Re: using sprintf

Posted: Sat Oct 19, 2013 5:02 am
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

Re: using sprintf

Posted: Sat Oct 19, 2013 5:25 am
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.

Re: using sprintf

Posted: Sat Oct 19, 2013 5:55 am
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.

Re: using sprintf

Posted: Sat Oct 19, 2013 6:15 pm
by requinix
If it didn't insert then there was an error. Regardless of whether you actually check for it.
requinix wrote:mysql_error

Re: using sprintf

Posted: Sat Oct 19, 2013 6:19 pm
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

Re: using sprintf

Posted: Sat Oct 19, 2013 7:02 pm
by requinix
requinix wrote:
requinix wrote:mysql_error