Help! Mysql Insert Query Wont Work!

liunx

Guest
Please help! I can't figure out why this code isn't working. <br />I made my column names in my mySQL table with spaces in them, and now I'm getting syntax errors on an INSERT query.. I tried escaping out quote characters and everything.. I must be doing something wrong. <br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$query = "INSERT INTO contacts (Date Entered, Date Of Call, Company Name, Contact Name, Phone, Contact, Source, Result) VALUES ('$dateent','$datecall','$compname','$contname','$phone','$contact','$source','$result')";<!--c2--></div><!--ec2--><br /><br />I'd appreciate any help on the subject.<br />Thanks!<br /><br />Sarah<!--content-->
That's something I never thought about but... can spaces be used at all??<br /><br />Maybe that's the problem... <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/unsure.gif" style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /><!--content-->
I've never tried mysql columns with spaces. You might want to try it without to see if your script works.<!--content-->
Well, I couldn't figure it out.. so I changed the column names.<br /><br />But I wanted them to show up like this on the screen:<br /><br />"Sorting by: Date Of Call "<br /><br />but now since I couldn't use spaces, It looks like this:<br /><br />"Sorting by: Date_Of_Call " . Kinda unprofessional.. I'll have to write more code to change the variables I guess.<br /><br /><br />Sarah<!--content-->
just a guess as I've never tried this in MySQL specifically, but...<br /><br />When crafting a SQL statement, you can use a column name definition in the format:<br /><br />SELECT column_name AS 'Desired Name' FROM table_name;<br /><br />I don't know the syntax off the top of my head for denoting that Desired name. It may need to be either surrounded by ", ', or [].<br /><br />Hope that helps!<!--content-->
1stover has a good idea worth checking into. <br /><br />But it sounds like you just need to change a variable in your script so that the echo or print statement says something different than it currently does.<!--content-->
Jack...I don't know a lick of PHP or most other scripting languages for that matter. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" /> Your suggestion is probably a much cleaner resolve than mine.<!--content-->
Thanks 1stover... your suggestion was good too.<br /><br />Sarah, look in your script for "Sorting by:" so that you can find the line that prints out this statement. Once you find it, you shouldn't have too much problem identifying the variable name that actually prints out the column name. <br /><br />One way to get a cleaner version would be to do a replace statement that removes certain characters.<br /><br />So let's say the variable is $column and the print statement looks something like print("Sorting by: $column");<br /><br />But you're getting stuff like:<br /><br />Sorting by: Date_Of_Call<br /><br />Then you could do this:<br /><br />$new_column = str_replace("_"," ", $column);<br />print("Sorting by: $new_column");<br /><br />This keeps the value of the first variable the same and keeps your code intact but also creates a new variable that replaces all "_" with a space. This way, you can add as many columns as you like and the code will still work perfectly.<br /><br />There you go!<!--content-->
Ah, now that's clever coding! Go Jack!<br /> <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/cool.gif" style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> <br /><br />(PS: It's LStover. The Arial type font lowercase L is pretty lame! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/rolleyes.gif" style="vertical-align:middle" emoid=":rolleyes:" border="0" alt="rolleyes.gif" /> )<!--content-->
That's some good code to use.. I think I will try that when I clean it up.<br /><br />But for now (to get it working temporarily) I just changed the column names back to "Date_Of_Call" with the underscores and then wrote this code to make it display differently:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if ($sortby == "Date_Of_Call"){<br />  $sortby_print = "Date Of Call";<br />}<!--c2--></div><!--ec2--><br /><br />That way, it'll just print the sortby_print variable instead of the column name.. <br /><br />But I will try your code, seems less redundant than coding all those if statements.<br /><br />Thanks for the ideas!<br /><br />Sarah<!--content-->
 
Top