eminempk Posted May 15, 2011 Share Posted May 15, 2011 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 <?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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/ Share on other sites More sharing options...
Labrat Posted May 15, 2011 Share Posted May 15, 2011 what are you trying to insert? needs to be in the form of INSERT into info ( ) values('') Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1215738 Share on other sites More sharing options...
eminempk Posted May 15, 2011 Author Share Posted May 15, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1215764 Share on other sites More sharing options...
Labrat Posted May 15, 2011 Share Posted May 15, 2011 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1215813 Share on other sites More sharing options...
fugix Posted May 16, 2011 Share Posted May 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1215837 Share on other sites More sharing options...
fenway Posted May 16, 2011 Share Posted May 16, 2011 And no more code without code tags. Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1215861 Share on other sites More sharing options...
eminempk Posted May 16, 2011 Author Share Posted May 16, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1216009 Share on other sites More sharing options...
Maq Posted May 16, 2011 Share Posted May 16, 2011 @eminempk, as fenway mentioned, when posting code place tags around it. Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1216052 Share on other sites More sharing options...
eminempk Posted May 16, 2011 Author Share Posted May 16, 2011 @eminempk, as fenway mentioned, when posting code place tags around it. Sorry Everyone i didnt know, from now on i will do tht. Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1216058 Share on other sites More sharing options...
PFMaBiSmAd Posted May 16, 2011 Share Posted May 16, 2011 If you had error_reporting set to E_ALL, you would find that $_POST['title'] does not exist. Looking at where that field is produced, you are missing an equal sign in the name='title' attribute. Quote Link to comment https://forums.phpfreaks.com/topic/236475-need-help-in-adding-the-content/#findComment-1216074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.