Jump to content

deciding whether to use single quotes or quotation marks around variables


Recommended Posts

I have a question about which to use around variables while sending a query to MS SQL.  If I try to use single quotations around a variable my users cannot use contractions (can't, don't etc.).  If I use quotation marks around my variable my users cannot use quotation marks.
Okay to give you an idea of what is goin on I have written an online program for my employer to hold a customer database, computers that come in to get fixed get logged in, and the techs use the program to update online what they have done to the computer so that the customers can check online the status of their computer.  So when the techs try to use an appostrophe the database fails.  Here's an example:

      $query = "Insert into pc_completed values (\"$customer_id\", \"$customer_lname\", \"$customer_fname\", \"$customer_address\", \"$customer_city\", \"$customer_state\", \"$customer_zip\", \"$customer_phone\", \"$alt_phone\", \"$admin_user\", \"$computer_problem\", \"$trc_store\", \"$customer_passwd\", \"$date_in\", \"$date_completed\", \"$customer_call_1\", \"$customer_call_2\", \"$work_completed\", \"$work_completed_1\", \"$work_completed_2\", \"$tech_assigned\", \"$reason_for_call_1\", \"$reason_for_call_2\");";

Using the quotation marks around the variable the techs can now use contractions.  But they cannot use quotation marks in the program itself.  When PHP sends it to the database the query fails.  If anyone knows why or has a helpfull hint it would be greatly appreciated.
This should allow you to do handle anything in the fields:

[code=php:0]
$customer_id = str_replace("'","''",$customer_id);
$customer_lname = str_replace("'","''",$customer_lname);
$customer_fname = str_replace("'","''",$customer_fname);
$customer_address = str_replace("'","''",$customer_address);
$customer_city = str_replace("'","''",$customer_city);
$customer_state = str_replace("'","''",$customer_state);
$customer_zip = str_replace("'","''",$customer_zip);
$customer_phone = str_replace("'","''",$customer_phone);
$alt_phone = str_replace("'","''",$alt_phone);
$admin_user = str_replace("'","''",$admin_user);
$computer_problem = str_replace("'","''",$computer_problem);
$trc_store = str_replace("'","''",$trc_store);
$customer_passwd = str_replace("'","''",$customer_passwd);
$date_in = str_replace("'","''",$date_in);
$date_completed = str_replace("'","''",$date_completed);
$customer_call_1 = str_replace("'","''",$customer_call_1);
$customer_call_2 = str_replace("'","''",$customer_call_2);
$work_completed = str_replace("'","''",$work_completed);
$work_completed_1 = str_replace("'","''",$work_completed_1);
$work_completed_2 = str_replace("'","''",$work_completed_2);
$tech_assigned = str_replace("'","''",$tech_assigned);
$reason_for_call_1 = str_replace("'","''",$reason_for_call_1);
$reason_for_call_2 = str_replace("'","''",$reason_for_call_2);

$query = "Insert into pc_completed values ('$customer_id', '$customer_lname', '$customer_fname', '$customer_address',
'$customer_city', '$customer_state', '$customer_zip', '$customer_phone', '$alt_phone', '$admin_user', '$computer_problem',
'$trc_store', '$customer_passwd', '$date_in', '$date_completed', '$customer_call_1', '$customer_call_2', '$work_completed',
'$work_completed_1', '$work_completed_2', '$tech_assigned', '$reason_for_call_1', '$reason_for_call_2')";
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.