PHP Insert

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

PHP Insert

Post by danarashad »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello again
I am trying to insert a datetime from a form field.

Code: Select all

<input type="hidden" value="<? echo date("m-j-y g:i:s A"); ?>" name="time" />
$time=$_POST['time'];
the insert code is.

Code: Select all

$sql="INSERT INTO $tbl_name(username, password, level, email,fname,lname,time)
VALUES('$uname', '$pword', '$level','$email','$fname','$lname','$time')";
but i only receive the word error. if i take out the time, i can insert just fine.

in mysql it is set up as timestamp. help please


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
IMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

TIMESTAMP is the way VladSun described, or an actual timestamp if you are using an older version of MySQL (I believe 4.x and below.. maybe 3.x). It'd never be "m-j-y" or have the ante/post meridian.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It may be better to keep this timestamp stored in a session variable so it cannot be manipulated by the user. Also, if you're not aware, a timestamp field, in MySQL at least, automatically updates when the record is updated (unless you set the field during that update.)
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

Multiple table insert

Post by danarashad »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok I am having a problem, I am trying to do an insert into two different tables.  No matter what I've tried it doesn't work.
Hopefully i've formatted this right, so I dont get in trouble with the format police.
but what happens i add a user for a form. 
there are check boxes that i have assigned.  i can insert the user's info name, etc.  but when it gets to the second insert, it will go through the loop and echo out everything to the screen, but it will not insert.  what am i doing wrong.  the loop does work, i get the out put, with the correct values.

Code: Select all

< ?  php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="password"; // Mysql password 
$db_name="user"; // Database name 
$tbl_name="user"; // Table name 
$tbl_name1="access";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$lname=$_POST['lname'];
$fname=$_POST['fname']; 
$uname=$_POST['uname'];
$pword=$_POST['pword'];
$email=$_POST['email'];
$level=$_POST['level'];
$jobtitle=$_POST['jtitle'];
$time=$_POST['time'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(username, password, level, email,fname,lname,jobTitle,time)VALUES('$uname', '$pword', '$level','$email','$fname','$lname','$jobtitle','$time')";
$result=mysql_query($sql);


? >
second insert
i requery the DB to get the matching criteria. that works the insert doesnt. it will out put the correct id, and the correct values for my array, it just will not insert help.

Code: Select all

< ? php
echo "time $time <br>";
$username="root";
$password="password";
$database="user";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM user where time='$time'";
$result=mysql_query($query);

$num=mysql_num_rows($result);
$a=0;

while ($a < $num) {
$uid=mysql_result($result,$i,"id");
print " User that was just inserted $uid <br>";
++$a;
}

if(isset($_POST["test"])) {$test = $_POST["test"];} else {$test=array();} 

for ($i="0"; $i<count($test); $i++) 
{
	if(!is_numeric($test[$i])) {$test[$i]="";} 
	if(empty($test[$i])) {unset($test[$i]);}
	$query="INSERT INTO access (userid, page_id)VALUES('$uid','$test[$i]')";
	echo "<br>first insert $uid $test[$i] <br>";
	
} 

? >

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply