Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,849 members, 7,810,274 topics. Date: Saturday, 27 April 2024 at 04:17 AM

Php/mysql Insert Issues - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Php/mysql Insert Issues (1070 Views)

How To Make a DESKTOP Executable Software Using PHP,MYSQL,SQLITE / Insert Image Into Mysql / Php/mysql Help:how To Run Sql Query From A Link (2) (3) (4)

(1) (Reply)

Php/mysql Insert Issues by IronFist(m): 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)

A Java Programmer To Be Needs Your Help Please! / THE VALLEY OF DEATH - A PLACE YOU'LL FIND MOST FOUNDERS / Voice Advertisment To Cell Phone

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.