₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,058 members, 8,420,082 topics. Date: Thursday, 04 June 2026 at 11:02 AM

Toggle theme

Php/mysql Insert Issues - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPhp/mysql Insert Issues (1180 Views)

1 Reply

Php/mysql Insert Issues by IronFist(op): 11:18am On Mar 20, 2007
PHP/MySQL gurus.I need help with PHP/MySQL. I created a form but every time i send stuff through the form it doesn't insert nothing.I'm attaching the SQL for my table
CREATE TABLE  `albion`.`article` (
`ID` int(6) unsigned NOT NULL auto_increment COMMENT 'The unique ID of the article',
`topic` varchar(200) collate latin1_general_ci default NULL COMMENT 'The article title',
`author` varchar(255) collate latin1_general_ci default NULL COMMENT 'Short summary of the article',
`article` text collate latin1_general_ci COMMENT 'The article itself',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Here's the code for my PHP form


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Create Article</title>
<link href="terracotta.css" rel="stylesheet" type="text/css" />
</head>
<?
include_once ', /includes/Dbconnect.php';
if(isset($_POST['Save Article']))
{
$action=$_POST['Save Article'];
$connector = new Dbconnect();
$insertQuery = "INSERT INTO article (topic,author,article) VALUES("."'".$_POST['Topic']."',".
"'".$_POST['Author']."',".
"'".$_POST['Article']."')";
if ($result = $connector->query($insertQuery))
{
echo '<center><b>Article added to the database</b></center><br>';
}
else
{
//It hasn't worked. check your input again.
exit('<center>Sorry, there was an error saving to the database</center>');
}
}


?>
<body>
<table width="677" border="0" cellpadding="0" cellspacing="0" align="center" height="500">

<tr>
<td height="69" colspan="2" valign="top" ><center><? include_once(", /header.php"wink;?></center></td>
</tr>
<tr>
<td width="114" height="175" valign="top" >
<form action="newArticle.php" method="post" enctype="multipart/form-data">
<table width="50%" border="0" align="center" cellpadding="10" bgcolor="#FFFFFF" class="put" hspace="5">
<tr ><td bgcolor="">Topic</td><td><input type="text" id="Topic" /></td></tr>
<tr>
<td>Author</td>
<td><input type="text" id="Author" /></td></tr>
<tr><td>Article</td>
<td><textarea name="textarea" cols="50" rows="5" name="article"></textarea></td>
</tr>
<tr><td><input type="submit" name="Save Article" value="Save Article" /></td><td><input type="reset" value="Reset" /></td></tr>
</table>
</form>
</td>
</tr>
<tr>
<td height="87" colspan="2" valign="top"><? include(", /footer.php"wink;?></td>
</tr>
</table>
</body>
</html>

What exactly am i getting wrong?Appreciate the help fellaz.
Re: Php/mysql Insert Issues by oleku(m): 4:01pm On Mar 26, 2007
I have been able to edit your code. Try this :

<?

if(isset($_POST['Save Article']))
{

$db = mysql_connect($host,$username,$password); // Databse Host Username and password
mysql_select_db($database,$db); // Database name

$Query = sprintf("INSERT INTO article (topic,author,article) VALUES('%s','%s','%s')",$_POST['Topic'],$_POST['Author'],$_POST['Article']);

if ($result = mysql_query($Query))
{
echo '<center><b>Article added to the database</b></center><br>';
}
else
{
exit('<center>Sorry, there was an error saving to the database</center>');
}
}


?>



oleku
www.oleku.org
Re: Php/mysql Insert Issues by naijaguru(m): 4:41pm On Mar 27, 2007
The code posted by Oleku should work but. However one needs to clarify somethings here.

IronFist. You were trying to use the DBconnect object from PEAR, but u didnt seem to understand how it works. You should try avoiding using PEAR if u are a rookie in PHP. Use the 'native' mysql_ functions in php

1. To create table/database in PHP, use PhpMyAdmin - as a starter.

2. To Insert data, I'd advice u to run your queries this way.

$sql = "INSERT INTO table SET field1='value1', field2 = 'value2', field3='value3'";
instead of
$sql = "INSERT INTO table (field1, field2, field3) VALUES (value1, value2, value3)";

3. And make sure u always use the addslashes() function for your queries and stripslashes() for output
1 Reply

How To Make a DESKTOP Executable Software Using PHP,MYSQL,SQLITEInsert Image Into MysqlPhp/mysql Help:how To Run Sql Query From A Link234

C#.net Fans:Computer Networking Guys Take A Look At This Android AppHas Anyone Tried The New Sublime-text 3 Beta version?