Jump to content

[SOLVED] Excluding Quotes (") from a MYSQL query to reduce problems


jonoc33

Recommended Posts

Hey guys.

 

I have a script which adds something to a database through a form. Whenever I add something that has quotes in it (") it causes the MySQL to have problems. What script would I need to remove quotes from the submitted form? Or is there any way to stop problems from happening but without removing the quotes?

 

<?php
include("config.php");

$con = mysql_connect("localhost","*****","***");
if (!$con)
  die('Could not connect: ' . mysql_error());
mysql_select_db("*****", $con);
$sql="INSERT INTO alpha_tactics (tacticname, map, tactics)
VALUES ('" . $_POST["tacticname"] . "','" . $_POST["map"] . "','" . stripslashes($_POST['tactics']) . "')";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br>" . mysql_error);
echo "<center>Record added.";
echo "<center><a href=admin.php>Back</a>";

?> 

 

Jono

Link to comment
Share on other sites

Try this

 

<?php
$db_host = 'localhost';
$db_pass = 'abc123';
$db_user = 'bob_jones';
$db_table = 'some_table';
include("config.php");
if(mysql_connect($db_host,$db_user,$db_pass)) {
	if(mysql_select_db($db_table)) {
		$tactic_name = mysql_real_escape_string($_POST['tacticname']);
		$tactic_map = mysql_real_escape_string($_POST["map"]);
		$tactic = mysql_real_escape_string($_POST['tactics']);
		$tactic_rows = array(tacticname,map,tactics);
		$tactic_values = array("'" . $tactic_name . "','" . $tactic_map . "','" . $tactic . "'");
		if(mysql_query("INSERT INTO alpha_tactics (" . $tactic_rows . ") VALUES (" . $tactic_values . ")")) {
			print '<center>Record added. <a href=admin.php>Back</a> </center>;
		}
		else {
			print mysql_error();
		}
	}
	else {
		print mysql_error();
	}
}
else {
	print mysql_error();
}
?> 

Link to comment
Share on other sites

I messed around with it and it works now (adds slashes). Although.

 

I have a script that gives the user an EDIT button to edit the tactic.

 

 print "<form name=\"form2\" method=\"post\" action=\"tacticedit1.php\">";
 print "<input type=\"submit\" name=\"button\" id=\"button\" value=\"Edit ".$values['tacticname']."\">";
     print "<input name=\"tacticname\" type=\"hidden\" id=\"tacticname\" value=\"".$values['tacticname']."\">";
 print "<input name=\"map\" type=\"hidden\" id=\"map\" value=\"".$values['map']."\">";
 print "<input name=\"tactics\" type=\"hidden\" id=\"tactics\" value=\"".$values['tactics']."\">";
     print "</form>";

 

Notice the last input how it has the Tactics in it. They are all hidden because they are needed for it to be edited.

Now, say if I made a tactic and put this into it:

 

<img src="vr.jpg" />

 

It will put it in right, but when you go and look at the edit button, under it it has

 

">

 

And if you hit the edit button, the picture will not show up.

 

What I still dont understand is how it still has problems even when it's added slashes.

 

Link to comment
Share on other sites

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.