Page 1 of 1

INSERTING VALUES

Posted: Thu Jun 14, 2007 9:22 am
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>

Posted: Thu Jun 14, 2007 9:44 am
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

Posted: Thu Jun 14, 2007 10:38 am
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')

Posted: Thu Jun 14, 2007 12:04 pm
by franknu
I want current date and time

session, with the varch. it it already declare

Posted: Thu Jun 14, 2007 3:51 pm
by RobertGonzalez

Code: Select all

INSERT INTO `myTable` SET `myDate` = NOW();
This is a MySQL syntax.

Posted: Fri Jun 15, 2007 8:55 am
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.

Posted: Fri Jun 15, 2007 9:02 am
by JayBird
alimadzi wrote:
I don't think that would work. You seem to be mixing INSERT and UPDATE syntax.
Both are, infact, correct

Posted: Fri Jun 15, 2007 9:40 am
by alimadzi
JayBird wrote:Both are, infact, correct
Very interesting. I sit corrected.