Jump to content

Recommended Posts

i need help in tht the following code, it doesnt add the content in the database, i dnt noe wht went wrong in the code  :-[ need help , i need help in the add content function, i guess something is wrong in the loop bt cnt figure it out wht  :shrug:

<?php

class database_connect {

var $host;
var $username;
var $password;
var $con;
var $db;

function connect() {

	$con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
	mysql_select_db($this->db, $con) or die(mysql_error());
}
function get_content($id ='') {
     
	if($id != ''):
	$id = mysql_real_escape_string($id);
	$sql= "SELECT * FROM info WHERE id = '$id'";
	$ret = '<p><a href="index.php">Go back to content</a></p>';
	else:
	$sql= "SELECT * FROM info ORDER BY id DESC";
	endif;

	$res = mysql_query($sql) or die (mysql_error());

	if(mysql_num_rows($res) != 0):
	while ($row = mysql_fetch_assoc($res)){
		echo '<h1> <a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a> </h1>';
		echo '<p>' . $row['body'] . '</p>';

	}
	else :
	echo '<p>doesnt exist</p>';
	endif;
	echo $ret;

}
function add_content($p) {
	$title = mysql_real_escape_string($p['title']);
	$body = mysql_real_escape_string($p['body']);

	if(!$title || !$body):
	if(!$title):
	echo "<p>The title Is Required</p>";
	endif;
	if(!$body):
	echo "<p>The body Is Required</p>";
	endif;


	echo '<p><a href="add-content.php">Try Again</a></p>';
	else:	

$sql = "INSERT INTO info  VALUES (null, '$title' , '$body')";
$res = mysql_query($sql) or die(mysql_error());
echo "Added Sucessfully";
endif;

}
//End of class

}
?>

Link to comment
https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/
Share on other sites

what are you trying to insert?  needs to be in the form of INSERT into info ( ) values('')

 

Any data whn i try to insert it says tell me "The title Is Required" nd it didnt add the data into the database whn i retrieve the data, here is the code for add_content.php so tht u can see the whole picture.

<?php
include "connect.php";
$obj = new database_connect();

//Setup our Database Variables
$obj->host = 'localhost';
$obj->username = 'root';
$obj->password = '';
$obj->db = 'school';

//Connect to Our DB

$obj->connect();
?>





<!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=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Modern CMS</title>
</head>

<body>
<div id="page-wrap">
<?php
include 'nav.php';


?>
<form method="post" action="index.php">
<input type="hidden" name="add" value="true" />
<div>
<label for="title">Title:</label>
<input type="text" name"title" id="title" />
</div>

<div>
<label for = "body">Body:</label>
<textarea name="body" id="body" rows="8" cols="40">
</textarea>

</div>
<input type="submit" name="submit" value="Add Content" />
</form>
</div>
</body>
</html>

like i said, look at your insert statement, the query doesnt now where to insert the submitted data.

 

$sql = "INSERT INTO info  VALUES (null, '$title' , '$body')";

 

should be

 

$sql = "INSERT INTO info (title,body) VALUES ('$title' , '$body')";

 

also need to add a " = " to this line

<label for="title">Title:</label>

<input type="text" name"title" id="title" />

like i said, look at your insert statement, the query doesnt now where to insert the submitted data.

 

$sql = "INSERT INTO info  VALUES (null, '$title' , '$body')";

 

should be

 

$sql = "INSERT INTO info (title,body) VALUES ('$title' , '$body')";

 

also need to add a " = " to this line

<label for="title">Title:</label>

<input type="text" name"title" id="title" />

labrat, you do not need to specify where to put the values as long as the values match with the fields, if you have an auto-incrementing field that you are not inserting a value into, you will need to put empty quotations for that field in your insert clause...eminempk, where is the script that is calling the functions that you created?

like i said, look at your insert statement, the query doesnt now where to insert the submitted data.

 

$sql = "INSERT INTO info  VALUES (null, '$title' , '$body')";

 

should be

 

$sql = "INSERT INTO info (title,body) VALUES ('$title' , '$body')";

 

also need to add a " = " to this line

<label for="title">Title:</label>

<input type="text" name"title" id="title" />

labrat, you do not need to specify where to put the values as long as the values match with the fields, if you have an auto-incrementing field that you are not inserting a value into, you will need to put empty quotations for that field in your insert clause...eminempk, where is the script that is calling the functions that you created?

 

@Fugix this is the code that is calling those functions...

 

 

<?php
include "connect.php";
$obj = new database_connect();

//Setup our Database Variables
$obj->host = 'localhost';
$obj->username = 'root';
$obj->password = '';
$obj->db = 'school';

//Connect to Our DB

$obj->connect();
?>

<!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=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Modern CMS</title>
</head>

<body>
<div id="page-wrap">
<?php
include 'nav.php';
?>
<?php
if ($_POST['add']):
$obj->add_content($_POST);
endif;
?>


</div>
</body>
</html>

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.