Sunday, 15 February 2015

redirect to php page from sql server fetch array loop -



redirect to php page from sql server fetch array loop -

i'm trying redirect php page sql server fetch array while loop. records fetched well, want add together table "delete" link every fetched record. tried code below separatedly , works fine. problem because code within php tag , i'm confused of ' ' signs. while compailing code below can't see nil (without "delete" line ok):

while ( $record = sqlsrv_fetch_array($rs, sqlsrv_fetch_numeric) ) { $o .= '<tr><td><center>'.$record [0].'</center></td><td><center>'.$record [1].'</center></td> <td><center>'.$record [2].'</center></td><td><center>'.$record [3].'</center></td> <td><center>'.$record [4].'</center></td><td><center>'.$record [5].'</center></td> <td><center>'.$record [6].'</center></td><td><center>'.$record [7].'</center></td> <td>**<a href="javascript:if(confirm('delete record?')==true) {window.location='phpsqlserverdeleterecord.php?eventid=<?php echo $record[0];';}">delete</a>** </tr>'; } $o .= '</tbody></table>'; echo $o;

thanks lot help!

the utilize of single-quotes creating syntax error:

'</center></td><td>**<a href="javascript:if(confirm('delete record?')==true) {window.location='phpsqlserverdeleterecord.php?eventid=<?php echo $record[0];';}">delete</a>** </tr>'

since server-side string literal began single quote, string contains single-quote php engine interpret end of string, results in couple of syntax errors here things intended part of string interpreted code.

there couple of ways go this. 1 quick prepare "escape" single-quotes meant part of string rather boundary of string:

'</center></td><td>**<a href="javascript:if(confirm(\'delete record?\')==true) {window.location=\'phpsqlserverdeleterecord.php?eventid=<?php echo $record[0];\';}">delete</a>** </tr>'

of course, there's still error php code (the echo statement) beingness emitted part of string. can terminate string boundaries around that , concatenate value string other string concatenation:

'</center></td><td>**<a href="javascript:if(confirm(\'delete record?\')==true) {window.location=\'phpsqlserverdeleterecord.php?eventid=' . $record[0] . '\';}">delete</a>** </tr>'

(note after reference $record[0] there two single-quotes. 1 begin new string literal , escaped 1 first character in string literal, since client-side code need single-quote character terminate its string literal. that's root of confusion in matter... these server-side strings aren't html, they're javascript has own client-side strings.)

you'll want url-encode value beingness emitted part of query string (even though should numeric identifier suspect, it's still form):

'</center></td><td>**<a href="javascript:if(confirm(\'delete record?\')==true) {window.location=\'phpsqlserverdeleterecord.php?eventid=' . urlencode($record[0]) . '\';}">delete</a>** </tr>'

there number of other ways potentially clean up. take @ the php documentation on strings, particularly around "heredoc" syntax. php rusty whip illustration of that, suspect end looking little cleaner in resulting code. either way, it's practice in differences between various ways php handle string literals.

php sql

No comments:

Post a Comment