INSERTING VALUES

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

INSERTING VALUES

Post by franknu »

Ok. I want to insert values into a table my problem is that

I am gettin this error: Duplicate entry '' for key 1

I also want to insert some other values like a session and the date with the time

please help here is my code

Code: Select all

<?PHP

  $BusinessName = addslashes($_POST['BusinessName']);
  $date= addslashes($_POST['date']);
  $from = addslashes($_POST['from']);
  $status= addslashes($_POST['status']);
  $subject= addslashes($_POST['subject']);
  $message= addslashes($_POST['message']);


if(isset($_SESSION['BusinessName'])){
$query = "INSERT INTO  `messages` (`BusinessName`,`date`,`from`,`status`,`subject`,`message`)
VALUES ('".$_SESSION['BusinessName']."','".$date."','".$from."', '".$status."','".$subject."',
'".$message."')";

$result = mysql_query($query);
echo mysql_error();

if($result)
         {
echo mysql_affected_rows()." .Your Message have been sent. We will get back to you. <br>";
          }
}

?>


<center>

<?
$_SESSION['BusinessName'];
?>


<? echo'<form action="'. $_SERVER['PHP_SELF'].'" method="post" >'; ?>

 <table width="390" border="1" bordercolor="#FFFFFF">
                            <tr>
                              <td width="92" bgcolor="#CCCCCC"><strong>From</strong></td>
                              <td width="282"><? echo'<input name="from"  type="text" size="60">'; ?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Subject </strong></td>
                              <td><? echo'<input name="textfield2" type="subject"  size="60">'; ?></td>
                            </tr>
                            <tr>
                              <td bgcolor="#CCCCCC"><strong>Body</strong></td>
                              <td><p>
                                <? echo'<textarea name="message" cols="75" rows="10"></textarea>'; ?>
                              </p>
                                <p>&nbsp;</p></td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                              <td bgcolor="#EFEFEF"><div align="right">
                                <table width="200" border="0" bgcolor="#EFEFEF">
                                    <tr>
                                      <td><? echo'<input type="submit" name="Submit2">'; ?></td>
                                      <td><? echo'<input type="submit" name="Submit" >'; ?></td>
                                    </tr>
                                                              </table>
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

Ok I fixed the duplicate my problem was that the id was not autoincrementing

so my next problem is how do i insert the date and the session into the database
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

1. Session.
Just create VARCHAR(32) field in your `messages` table and insert session_id() in this field
2. Date
What date would you like to insert? Current? In this way you can use usual DateTime format in DB and date('F-m-d H:i:s')
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

I want current date and time

session, with the varch. it it already declare
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

INSERT INTO `myTable` SET `myDate` = NOW();
This is a MySQL syntax.
User avatar
alimadzi
Forum Newbie
Posts: 9
Joined: Fri Jun 08, 2007 12:57 pm
Location: Boise, Idaho, USA

Post by alimadzi »

Everah wrote:

Code: Select all

INSERT INTO `myTable` SET `myDate` = NOW();
This is a MySQL syntax.
I don't think that would work. You seem to be mixing INSERT and UPDATE syntax. Here's INSERT:

Code: Select all

INSERT INTO MyTable (column1, column2) VALUES ('foo', 'bar')
And here's UPDATE:

Code: Select all

UPDATE MyTable SET column1 = 'foo', column2 = 'bar' WHERE id = '1'
And here's the documentation:

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

You are correct about the use of NOW() to get the current time though.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

alimadzi wrote:
I don't think that would work. You seem to be mixing INSERT and UPDATE syntax.
Both are, infact, correct
User avatar
alimadzi
Forum Newbie
Posts: 9
Joined: Fri Jun 08, 2007 12:57 pm
Location: Boise, Idaho, USA

Post by alimadzi »

JayBird wrote:Both are, infact, correct
Very interesting. I sit corrected.
Post Reply