Jump to content

right syntax to use error


tweaka

Recommended Posts

•your MySQL server version -- absolutely required! MySQL5.1.52

•the raw MySQL statement in question [in a CODE block, and without any PHP variables]

 

•any errors that MySQL returns to the client [from mysql_error()] Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('business name','6091234567','')' at line 1

 

•the table structure & column indexes of the relevant tables [via SHOW CREATE TABLE is preferred]

•the EXPLAIN output for your query, if applicable

•a clear and concise description of what you want this statement to achieve

•a description of what it's currently doing that's not to your liking

•a brief listing of the types of things you've attempted so far

I am just starting out so I am pasting alot. The white box for entering content for this thread is very frustrating to use. I cant see what I am typing right now. The scrolling is broken.

Here is my code so far.

<?php

$dbhost = 'localhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business (business_name,telephone,contact_person,street_address,)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$POST[street_address]')";

 

if (!mysql_query($sql,$conn))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

 

mysql_close($conn)

?>     

Link to comment
Share on other sites

Query string: INSERT INTO business (business_name,telephone,contact_person,street_address,) VALUES ('business name','6091234567','')

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('business name','6091234567','')' at line 1

 

Link to comment
Share on other sites

The query is missing a value somehow. You list 4 fields into which to insert data, but there are only 3 values in the query, one of which is an empty string. The code you have in the OP can't be the current code that is executing.

 

At any rate, if it is, I spotted one problem: $POST['street_address'] is missing an underscore. $_POST . . .

 

This is going to ultimately end up as a PHP issue, so moving the thread to PHP Coding Help . . .

Link to comment
Share on other sites

?4 fields into which to insert data, but there are only 3 values in the query?

revised...

<?php

$dbhost = 'localhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business (business_name,telephone,contact_person,street_address,)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "1 record added";

 

mysql_close($con)

?>     

Link to comment
Share on other sites

?4 fields into which to insert data, but there are only 3 values in the query?

 

Yes. From where you pasted the query being echoed with the mysql_error():

INSERT INTO business 
(business_name,telephone,contact_person,street_address,)  // Four fields into which to insert
VALUES 
('business name','6091234567','')  // Three values, the third being an empty string ''

 

See how something isn't adding up here? The code you've posted is not the code that's being executed.

Link to comment
Share on other sites

This worked, it said 1 record added<?php

$dbhost = 'dbhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business

 

(business_name,telephone,contact_person,street_address,city_long,description_long)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]','$_POST

 

[city_long]','$_POST[description_long]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "1 record added";

 

mysql_close($con)

?> 

 

Wow, I didnt think syntax was so critcal.    Why when I am typing into this, I cant see what I am typing because the scrolling of this white form box doesnt let me ? Its soo anoying.

I have more to learn about this, where might I go and what should I study next if I were interested in using a database to generate html content ?

Thank you for helping me with this first step. yikes !

Link to comment
Share on other sites

The input box problem, as far as I can tell, seems to be a browser issue. Most people who have problems with it are using IE8(?), I use Firefox and/or Safari and have no issues with it, but I still compose most messages in an editor (especially if there's code in it) then paste it in.

Link to comment
Share on other sites

This worked, it said 1 record added<?php

$dbhost = 'dbhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business

 

(business_name,telephone,contact_person,street_address,city_long,description_long)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]','$_POST

 

[city_long]','$_POST[description_long]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "1 record added";

 

mysql_close($con)

?> 

 

Wow, I didnt think syntax was so critcal.    Why when I am typing into this, I cant see what I am typing because the scrolling of this white form box doesnt let me ? Its soo anoying.

I have more to learn about this, where might I go and what should I study next if I were interested in using a database to generate html content ?

Thank you for helping me with this first step. yikes !

 

>> go next >> a book about php and mysql web development: http://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166/ref=sr_1_1?ie=UTF8&qid=1290713197&sr=8-1

 

One thing to note about books: always make sure they are not too outdated and ensure they have decent reader reviews because it is absolutely critical you not read outdated trash. Some bad habits are very hard to shake.

 

 

Link to comment
Share on other sites

Error: Unknown column 'backlink_1' in 'field list' when I basically expanded on the fields. I believe everything matches exactly. The first attempt worked, I figured this would also work. Sorry to bother you again, I thought I was done with this step.

 

<?php

$dbhost = 'localhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business

 

(business_name,telephone,contact_person,street_address,city,state_long,state_short,zipcode,description_short,description_long,sub_domain,backlink_1,backlink

 

_2,backlink_3,backlink_4,imageurl_small,imageurl_large)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]','$_POST[city]','$_POST[state_long]','$_POST[state_short]','_$

 

POST[zipcode]','$_POST[description_short]','$_POST[description_long]','$_POST[sub_domain]','$_POST[backlink_1]','$_POST[backlink_2]','$_POST[backlink_3]','$

 

_POST[backlink_4]','$_POST[imageurl_small]','$_POST[imageurl_large]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "Business Added for (info)";

 

mysql_close($con)

?>     

Link to comment
Share on other sites

Nope, I cant make it go...

Query string: INSERT INTO business (business_name,telephone,contact_person,street_address,city,state_long,state_short,zipcode,description_short,description_long,sub_domain,backlink_1,backlink_2,backlink_3,backlink_4,imageurl_small,imageurl_large) VALUES ('Plumber Service','(467)','Mr. Me Myself','123 Winding Way','New York','New York','NY','_','Short Description Here','Long Description Here','http://subdomain.','http://backlink1','http://backlink2','http://backlink3','http://backlink4','http://image','http://image')

Error: Unknown column 'backlink_1' in 'field list'

 

<?php

$dbhost = 'localhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business

 

(business_name,telephone,contact_person,street_address,city,state_long,state_short,zipcode,description_short,description_long,sub_domain,backlink_1,backlink

 

_2,backlink_3,backlink_4,imageurl_small,imageurl_large)

VALUES

('$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]','$_POST[city]','$_POST[state_long]','$_POST[state_short]','_$

 

POST[zipcode]','$_POST[description_short]','$_POST[description_long]','$_POST[sub_domain]','$_POST[backlink_1]','$_POST[backlink_2]','$_POST[backlink_3]','$

 

_POST[backlink_4]','$_POST[imageurl_small]','$_POST[imageurl_large]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "Business Added for (info)";

 

mysql_close($con)

?>     

Link to comment
Share on other sites

<?php

$dbhost = 'localhost';

$dbuser = 'dbuser';

$dbpass = 'dbpass';

 

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

 

$dbname = 'dbname';

mysql_select_db($dbname);

 

$sql="INSERT INTO business (business_type,business_name,telephone,contact_person,street_address,city,state_long,state_short,zipcode,description_short,description_long,domain,sub_domain,backlink1,backlink2,backlink3,backlink4,imageurl_small,imageurl_large)

VALUES

('$_POST[business_type]','$_POST[business_name]','$_POST[telephone]','$_POST[contact_person]','$_POST[street_address]','$_POST[city]','$_POST[state_long]','$_POST[state_short]','$_POST[zipcode]','$_POST[description_short]','$_POST[description_long]','$_POST[domain]','$_POST[sub_domain]','$_POST[backlink1]','$_POST[backlink2]','$_POST[backlink3]','$_POST[backlink4]','$_POST[imageurl_small]','$_POST[imageurl_large]')";

 

if (!mysql_query($sql,$con))

  {

die( "<br>Query string: $sql<br>Error: " . mysql_error() . '<br>');

  }

echo "Business Added for (info)";

 

mysql_close($con)

?>     

 

I am sure I found them all....

I am leaping toward my next steps, ahh which way  ?

I need to learn how to edit database existing info through html, and then I need a template html thing that can be placed in subdomains and still be able to access the db. I will check out the links given earlier to me, today. Right now they are bidding on this at Script Lance. I should be able to pull this off myself.

Link to comment
Share on other sites

Editing a database record is typically done by first SELECTing the record and displaying in in a form, then after any edits have been made, the form is submitted, data is validated and an UPDATE query is executed.

 

A very brief example would be:

<?php
// db connection stuff already done . . .
if( isset($_POST['submit']) ) {
$name = mysql_real_escape_string($_POST['name']);
$query = "UPDATE `table` SET `name` = '$name' WHERE user_id = 1";
if( $result = mysql_query($query) ) {
	echo mysql_affected_rows() . 'were updated successfully.';
}
} else {
$query = "SELECT `name` FROM `table` WHERE user_id = 1";
$result = mysql_query( $query );
$array = mysql_fetch_assoc( $result );
?>
<form action="" method="post">
<input type="text" name="name" value="<?php echo isset($array['name']) ? $array['name'] : ''; ?>"><br>
<input type="submit" name="submit" value="Update Record">
</form>
<?php
}
?>

Link to comment
Share on other sites

http://www.phpmyedit.org/article.php?download sorry, but in the end I am just an end user after all.

 

I was able to make files that can create database table, insert data , show that data and lastly edit it.

I am happy. It seems building dynamic html pages from a db might not do well for the kind of search engine results I am looking. I basically built a sort of rolodex to help keep track of what information I have and where it is within my site(s). Since it is subject to change, I thought it best to use a db to store the details instead of paper or txt files. I have many content managers running and dont use them for anything except fun. Simple machines Forum is my fav, wordpress is second, Sugar, Joomla, Drupal, ect ect...

My Palm Pre was able to run Wordpress, but at the cost of batt life.

 

If I wanted to catch the duplicate error and return a msg, or even redirect after a successful entry was made to another page, I wouldnt know how. This is where I stopped. But I accomplished my goal !  8) with help from phpfreaks. Now I know where to go for answers.

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.