Current location: Hot Scripts Forums » Programming Languages » PHP » Inserting data to mysql table using php

Inserting data to mysql table using php

Reply
  #1 (permalink)  
Old 09-28-06, 06:54 AM
Tjobbe Tjobbe is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Inserting data to mysql table using php

Right, a simple one, but I can't for the life of me figure it out, I've looked at a few dozen tutorials and code snippets but for some reason the data I want to enter will not display as I want it to.

Basically, I have a file with two fields, title and content, on submitting the form it goes to insert.php which "should" add the data to the table, but it does not.

I'd like the id to increase each time an entry is added and for the date to be autosubmitted too, is this possible?

The files are below, if anyone could point out the obvious then I'd be most grateful!

Tjobbe

index.php
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<
HTML>
<
HEAD>
<
TITLE> New Document </TITLE>
<
META NAME="Generator" CONTENT="EditPlus">
<
META NAME="Author" CONTENT="">
<
META NAME="Keywords" CONTENT="">
<
META NAME="Description" CONTENT="">
</
HEAD>

<
BODY>
<
form action="insert.php" method="post">
    
Title: <input type="text" name="title" /><br />
    
Content: <input type="text" name="content" /><br />
    <
input type="submit" />
</
form>
</
BODY>
</
HTML
insert.php
PHP Code:
<?

include "dbconnect.php";

$title="$_POST['$title]";
$content="$_POST['$Scontent]";

$sqlquery "INSERT INTO entries VALUES('$title','$content')";

$results mysql_query($sqlquery);

print 
"<html><body><center>";
print 
"<p>You have just entered this record<p>";
print 
"title : $title<br>";
print 
"content : $content<br>";
print 
"</body></html>";

?>
dbconnect.php
PHP Code:
<?php

$dbhost 
'localhost';
$dbuser 'root';
$dbpass 'inflight';

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

$dbname 'testing';
mysql_select_db($dbname);

?>
my table, the entries in there are what i added manually through phpmyadmin:
PHP Code:
-- phpMyAdmin SQL Dump
-- version 2.9.0-rc1
-- http://www.phpmyadmin.net
-- 
-- 
Hostlocalhost
-- Generation TimeSep 282006 at 11:53 AM
-- Server version5.0.24
-- PHP Version5.1.4
-- 
-- 
Database: `testing`
-- 

-- --------------------------------------------------------

-- 
-- 
Table structure for table `entries`
-- 

CREATE TABLE `entries` (
  `
idint(4NOT NULL auto_increment,
  `
titletext NOT NULL,
  `
contenttext NOT NULL,
  `
datedatetime NOT NULL,
  
PRIMARY KEY  (`id`)
ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

-- 
-- 
Dumping data for table `entries`
-- 

INSERT INTO `entries` (`id`, `title`, `content`, `date`) VALUES 
(13'fg adfg ae rth a''arth arth aetrh erg aehtra ergehet aretgh erg arehgaehteha ret\r\n\r\n\r\n<b>dafg adf ga</b>ad fg adfg adfg ad\r\na afdgadfg<br />RG ASDFGASDGASDFG''2006-09-28 11:22:51'),
(
14'yjt steyhj astr ytyjketyuawrt wytyw''rty wnrty wretwyty bwrty bwrtywrtywtry''2006-09-28 11:31:18'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-28-06, 06:58 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community VIP
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 2,724
Thanks: 0
Thanked 0 Times in 0 Posts
i think your problem is here:
PHP Code:
$title="$_POST['$title]";
$content="$_POST['$Scontent]"
you don't have to parse them as strings, as they are allready strings. Bu this s not causing the problem. You have a dollar sign before "Scontent" and "title", i think this has to be removed. this is what should be there:
PHP Code:
$title=$_POST['title'];
$content=$_POST['Scontent']; 
also add 'or die(mysql_error());' after every mysql_query(), that way you can check what mysql returns as error.

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

If you want to add me on any IM, pm me first
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-28-06, 07:07 AM
Tjobbe Tjobbe is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
I'm sure the error must be coming from the insert.php page, I've ammended the code a little:

PHP Code:
<?

include "dbconnect.php";

$sqlquery "INSERT INTO entries VALUES('id','title','content','date')";

$results mysql_query($sqlquery) or die(mysql_error());

print 
"<html><body><center>";
print 
"<p>You have just entered this record<p>";
print 
"title : $title<br>";
print 
"content : $content<br>";
print 
"</body></html>";

?>
I now get the following error on submitting the form: "Out of range value adjusted for column 'id' at row 1"

What does that mean? is the id and date fields not auto inserting? i know im not passing them, but ddont they get added automatically?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-28-06, 07:36 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community VIP
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 2,724
Thanks: 0
Thanked 0 Times in 0 Posts
you're insert query is wrong:
it should be:
PHP Code:
$query "INSERT INTO entries ('id', 'title', 'content', 'date') VALUES('', 'my_title', 'my_content', 'my_date')";
// or:
$query "INSERT INTO entries VALUES('', 'my_title', 'my_content', 'my_date')";
// or:
$query "INSERT INTO entries SET title='my_title', content='my_content', date='my_date'"
the reason why you got this error is because you were assigning a string to an integer type: id was an int(4) NOT NULL auto_increment, not a string

UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

If you want to add me on any IM, pm me first
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-28-06, 07:37 AM
Tjobbe Tjobbe is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
just this second fixed it, thanks for replying!

PHP Code:
<?

include "dbconnect.php";

$title=$_POST['title'];
$content=$_POST['content'];

$sqlquery "INSERT INTO entries VALUES(id,'$title','$content',NOW())";

$results mysql_query($sqlquery) or die(mysql_error());

print 
"<html><body><center>";
print 
"<p>You have just entered this record<p>";
print 
"title : $title<br>";
print 
"content : $content<br>";
print 
"</body></html>";

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Mysql Bug??? tranquilraven PHP 4 03-01-06 04:06 AM
Need help with some php mysql TheTinkeringToad PHP 9 02-01-06 11:56 AM
using ?id=$id from a mysql table in php kasper PHP 12 06-30-04 07:38 PM
cant add more than 1 mysql table with php script chrisb62 PHP 1 04-27-04 05:00 AM
Error when trying to create MySQL table via PHP HasansWeb PHP 2 01-06-04 06:13 PM


All times are GMT -5. The time now is 12:01 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.