 |
|
|
05-28-2005, 03:48 PM
|
Inserting into MYSql
|
Posts: 12
|
Hello, I am just learning MYSql so I'm not very good at it. I am having trouble inserting variables into the database.
<?php
include ('connect.php');
$_POST['title'] = addslashes($_POST['title']);
$_POST['news'] = addslashes($_POST['news']);
$sql = "INSERT INTO news (title, news)
VALUES ('".$_POST['title']."', $news = '".$_POST['news']."')";
Mysql_query($sql);
?>
It won't insert what I type into the textbox's title, and news. Instead it inserts something completely blank.
If anyone can help, thanks.
|
|
|
|
05-28-2005, 03:56 PM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
you should use seperate variables instead of updating the $_POST array
I also noticed a $news = in your insert statement, you need to remove that.
PHP Code:
$title = addslashes($_POST['title']);
$news = addslashes($_POST['news']);
$sql = "INSERT INTO news (title, news)
VALUES ('".$title."', '".$news."')";
Mysql_query($sql);
|
|
|
|
05-28-2005, 04:35 PM
|
|
Posts: 12
|
For some reason it still won't work. I'm sure what you corrected is all right, but I must still have something wrong, because it still inserts blank.
<html>
<body>
<form name=”newsubmit” method=”post” action=submit.php>
<br>
Title <input type="text" name="title"><br>
News<textarea name="news"></textarea><br>
<input type="submit" name="Submit" value="Submit">
</form>
</form>
</body>
</html>
thats my submit page
<?php
include ('connect.php');
$title = addslashes($_POST['title']);
$news = addslashes($_POST['news']);
$sql = "INSERT INTO news (title, news)
VALUES ('".$title."', '".$news."')";
Mysql_query($sql);
?>
That's my new, insert page.
If you could give me any more help, thanks alot.
|
|
|
|
05-28-2005, 04:40 PM
|
|
Posts: 253
Location: Constanta,Romania
|
Quote:
|
Originally Posted by iEye
<?php
include ('connect.php');
$title = addslashes($_POST['title']);
$news = addslashes($_POST['news']);
$sql = "INSERT INTO news (title, news)
VALUES ('".$title."', '".$news."')";
Mysql_query($sql);
?>
|
Try this code :
PHP Code:
<?php
include ('connect.php');
$title = addslashes($_POST['title']);
$news = addslashes($_POST['news']);
$sql = "insert into news set title = '$title',news = '$news'";
mysql_query($sql);
?>
|
|
|
|
05-28-2005, 04:46 PM
|
|
Posts: 12
|
Once again it inserts, but it is still blank...
|
|
|
|
05-28-2005, 04:59 PM
|
|
Posts: 12
|
I realized I can't even get this to work.
<?php
include ('connect.php');
$sql = 'INSERT INTO news (title, news) VALUES (test, test)';
mysql_query($sql);
?>
Does this mean something is wrong with connect.php?
|
|
|
|
05-28-2005, 05:16 PM
|
|
Posts: 253
Location: Constanta,Romania
|
It should give you an error if you don't use
PHP Code:
@mysql_connect(....)
and
@mysql_select_db(..)
if something was wrong with connect.php. But if the script inserts something complety blank i asume there is nothing wrong with connect.php .
|
|
|
|
05-28-2005, 05:19 PM
|
|
Posts: 253
Location: Constanta,Romania
|
Do you have shell access ? if you do try inserting manually some data into the database.. if it wont work there is something wrong with mysql and you should contact your webhost and let them know about this issue.
|
|
|
|
05-28-2005, 05:24 PM
|
|
Posts: 12
|
I tried manually inserting and it works.
<?php
// database info
$dbhost = "localhost";
$dbname = "xxxxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpass = "xxxxxxxxx";
// connect to the database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>
Thats my connect file.
|
|
|
|
05-28-2005, 08:16 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Is it putting in no records, or putting in blank records? Check your $_POST array using a few echo statements to see if it has got in it what you think it should have. Maybe it's empty for some reason.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
05-28-2005, 09:26 PM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
after you execute your insert statement, try this for debugging what's going on:
PHP Code:
echo "sql = $sql<br />";
echo "err = ". mysq_error();
This will allow you to check you $sql to make sure it is getting built right and values look like they should. You'll also see any errors that the DB spits out if there are any.
|
|
|
|
05-28-2005, 11:27 PM
|
|
Posts: 12
|
sql =
err = sql = INSERT INTO news (title, news) VALUES (test, test)
err = Unknown column 'test' in 'field list'
|
|
|
|
05-29-2005, 01:01 AM
|
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
Specifying values as <b>(test, test)</b> you refer to the column named <b>test</b> in your table. To insert the string 'test' you must write <b>values('test', 'test')</b>. In case of inserting variables it should be as following:
PHP Code:
$sql = "insert into news (title, news) values ('$title', '$news')";
|
|
|
|
05-29-2005, 09:17 PM
|
|
Posts: 12
|
sql =
err = sql = insert into news (title, news) values ('', '')
err =
So, apparently whatever I type in on the form, comes out blank...
|
|
|
|
05-30-2005, 05:49 AM
|
|
Posts: 253
Location: Constanta,Romania
|
Could be something wrong with php? try installing a forum and see if it works.. or another script wich has an autoinstaller and uses a sql db.
|
|
|
|
05-30-2005, 06:53 AM
|
|
Posts: 174
Location: Nigeria/Lagos
|
i guess there is something wrongn with the MySQL SERVER
__________________
Life is just lyke a school where everybody goes to learn one or two thing. the more u school, the more u learn more about school..The more we live our lifes.. the more we learn more about life.
Please login or register to view this content. Registration is FREE
|
|
|
|
05-30-2005, 01:38 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Nothing wrong with php or mySQL- if they were misconfigured you would not get errors of this type - it would either do nothing or complain about database names/passwords. All that is happening is that your form variables aren't being put into the query string - check your variable names, the names of your form controls etc, to make sure they all connect up. Use echo for debugging to find out whether variables have insode them what you think they should.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
05-30-2005, 03:44 PM
|
|
Posts: 12
|
$title = addslashes($_POST['title']);
$news = addslashes($_POST['news']);
I removed that, and now it works. Could someone please tell me what those 2 things do. Thanks for the help everyone.
|
|
|
|
05-30-2005, 10:36 PM
|
|
Posts: 15
Location: Lagos, Nigeria
|
try this :
<?
include "connect.php";
$title=addslashes($_POST['title']);
$news=addslahes($_POST['news']);
$sql=mysql_query("insert into news values('$title','$news')");
if($sql){
echo "news added ";
}else {
echo "errorr adding news ". mysql_error();
}
?>
remember :if you are having id column try to use this ' before the new variable in the values.
i have you this script for my insertion and its never failed.
hope it helps you.
|
|
|
|
|
« Reply to Inserting into MYSql
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|