Jump to content

[SOLVED] Adding records to the database


sando

Recommended Posts

I am trying to add an "add page" area to my cms.

 

Here is the code so far:

 

Add.php (The HTML form)

<?require_once "../includes/db.inc.php";
admin_session();?>

<form id="addform" name="addform" method="post" action="insert_page.php">
Page Title:
<input name="page_name" type="text" id="page_name" />
<input name="status" type="hidden" id="status" value="Active">
<input name="content" type="hidden" id="content" value="Default Page Content">
<input type="submit" name="Submit" value="Submit" />
</form> 

 

 

Insert.php (The process)

<?
// Connect database.
require_once "../includes/db.inc.php";
admin_session();

// Get values from form.
$page_name=$_POST['page_name'];
$status=$_POST['status'];
$content=$_POST['content'];

// Insert all parameters into database.
mysql_query("insert into static_page_name(page_name, status, ) values('$page_name', '$status', '$content')");

// Close database connection
mysql_close();

?>

 

 

The table is static_page_name containing:

static_page_id, content, page_name, status

 

 

When I submit the form it calls insert.php which opens leaving a blank screen. Nothing gets added to the database but no error messages show?

 

What am I doing wrong? Any help appreciated.

 

Thanks  :)

Link to comment
Share on other sites

mysql_query("insert into static_page_name(page_name, status, ) values('$page_name', '$status', '$content')") or die(mysql_error());

 

it will show you why it do not record to mysql table.

 

i do believe your mysql_query needs a connection as the secodn argument  like :

 

$conn = mysql_connect("localhost","name","password");

mysql_query($sql, $conn);

 

 

if didn't worked i must see your include codes.

 

 

 

 

 

Link to comment
Share on other sites

When I add the

or die(mysql_error()

 

I get this output:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') values('Test', 'Active', 'Insert Page Contents')' at line 1

Link to comment
Share on other sites

I have now added the connection directly to my file

 

$conn = mysql_connect("localhost","user","pass");
mysql_query($sql, $conn);

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') values('test1', 'Active', 'Insert Page Contents')' at line 1

Link to comment
Share on other sites

as it says you have a synax error :  after "," you have only a space,  you have 3 values , so you must give mysql 3 columns . or if you have 2 columns , give it 2 values .

your line should be at last :

 

 

(i removed your content from value parathes)

mysql_query("insert into static_page_name(page_name, status) values('$page_name', '$status')") or die(mysql_error());

 

or

 

(add a column to column parathes ) (i added the "content")

mysql_query("insert into static_page_name(page_name, status,content) values ('$page_name', '$status', '$content')") or die(mysql_error());

 

 

Link to comment
Share on other sites

Now working, thanks for the advise, the code at the end:

 

<?php
// Connect 2 db.
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$dbname = 'dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Connection Error');
mysql_select_db($dbname);

//Check for admin.
require_once "../includes/config.inc.php";
admin_session();

// Get form values.
$page_name=$_POST['page_name'];
$status=$_POST['status'];
$content=$_POST['content'];

//Enter info into db.
mysql_query("insert into web_page_name(page_name, status,content) values ('$page_name', '$status', '$content')") or die(mysql_error());
echo ("Done");

//finished.
mysql_close();
?>

Link to comment
Share on other sites

It might be worth noting that unless magic quotes are on (I hate magic quotes), that has the potential to syntax error if any input contains a '.

 

That would make it something like VALUES('The Page', 'Some Status', 'It's cold outside')

 

Without magic quotes, you would need to do addslashes() so It's cold outside would become It\'s cold out side.

Link to comment
Share on other sites

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.