Page 1 of 1

(Resolved) this is a simple question - DATE and MYSQL/PHP

Posted: Fri Aug 18, 2006 10:56 am
by Sid Rosenburg
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]


Am new to php/mysql  but I can figure things out reasonably quickly

however I have an issue that is crippling me 

I have a file with 9 fields all defined varchar except last field which is meant to be a date field.

when I insert data from a form   no matter what I do all I see is 0000-00-00 as the date stamp for this record 

I have used and tried the following

Code: Select all

$sql = "INSERT INTO players VALUES('$f1', '$f2',  '$f3', '$f4', '$f5', '$f6' ,'$f7', '$f8', NOW()) ";      
//$sql = "INSERT INTO players VALUES('$namefirst', '$namelast',  '$work', '$home', '$cell', '$email' ,'$comments', '$datechg') ";  
//$sql = "INSERT INTO players ( namefirst, namelast, work, home, cell, email, jpg, comments, datechg ) values ('$namefirst', '$namelast',  '$work', '$home', '$cell', '$email' , '$jpg', '$comments', '$datechg') "; 
 
//$datechg = curdate();
//$datechg = $_POST['date'];
$datechg = date("Y-m-d");
I am sure I am doing something really stupid please tell me what it is.

Any help would be appreciated


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]

Posted: Fri Aug 18, 2006 11:01 am
by feyd
Your insert query has nine fields not seven.

Posted: Fri Aug 18, 2006 11:16 am
by blackbeard
Can you show us the table structure? I'm wondering if the ninth field is the date field.

Posted: Fri Aug 18, 2006 11:22 am
by Sid Rosenburg

Code: Select all

Field           	Type  	Attributes  	Null  	Default  	Extra  	    Action
	namelast 	text 		No 			              Browse 	     Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	namefirst 	 text 		  No 			               Browse 	      Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	work 	           text 		No 			          Browse 	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	home 	          text 		    No 			                 Browse 	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	cell 	              text 		No 			             Browse 	    Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	email 	            text 		No 			           Browse 	 Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	jpg 	              text 	      No 			             Browse 	  Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	comments 	text 		No 			               Browse 	     Change 	Drop 	Primary 	Index 	Unique 	Fulltext
	datechg 	   date 	No 	0000-00-00 		Browse 	      Change 	Drop 	Primary 	Index 	Unique 	Fulltext

Posted: Fri Aug 18, 2006 6:30 pm
by RobertGonzalez
Are you doing any error checking?

Error checking

Posted: Fri Aug 18, 2006 6:54 pm
by Sid Rosenburg
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]


See the following for the html/php script  

The function populates a record in the file once its done just cant get the date to default to today.
Field datechg is defined as date 

hope this helps as I totally give up

Sid

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>xxxxxx</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<style TYPE="text/css"> 	
font      { font-family: Arial, Helvetica, sans-serif;  font-size: 12px}	
body      { font-family: Arial, Helvetica, sans-serif }	
table     { font-family: Arial, Helvetica, sans-serif }	
td        { font-family: Arial, Helvetica, sans-serif; font-size: 12px}	
p         { font-family: Arial, Helvetica, sans-serif }     
a:link {color:blue;}     
a:visited {color:black;}      
a:active {color:black;}     
a:hover {color:black;background:#94d6e7;}
</style>
<body>


<?php 
//$sql = "INSERT INTO players VALUES('$namefirst', '$namelast',  '$work', '$home', '$cell', '$email' ,'$comments', '$datechg') "; 

$sql = "INSERT INTO players ( namefirst, namelast, work, home, cell, email, jpg, comments, datechg ) values ('$namefirst', '$namelast',  '$work', '$home', '$cell', '$email' , '$jpg', '$comments', '$datechg') "; 

//  $sql = "INSERT INTO players ( namefirst, namelast, work, home, cell, email, comments ) values ('$namefirst', '$namelast',  '$work', '$home', '$cell', '$email' ,'$comments'  ";

$username = "xxxxx";
$password = "xxxxxx";
$hostname = "localhost"; 
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db ("biwrfcco_directory"); 
 


   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
<center>
<font face=arial>
<br><br><bR><br>
<table width="80%">
<tr><td><font face=arial size=2>
Purpose of this page is to allow you the internal Wolfhounds people to update or  add your contact details to the 411 directory.
What you enter wont appear pretty at first as there is some backend admin to do, but it will get better.
<br><br> A new feature will be to add a small business card listing in the comments section.
<br>This is version 1, so if you have services you want to offer the rest of the club ie 'frog catcher'  then make it known here.<br><br>
If you are not on the 411 list then use this tool to get there.<br><br>   
</td></tr>
</table>
<form name="form1" method="post" action="<?=$me?>">
<center>
<table>
<tr>
<td><font face=arial size=2>First Name  </td><td><input type="text" name="namefirst" size=20 maxlength=30> <font size=-1 color=red>* required</td></tr>
<tr>
<td><font face=arial size=2>Last Name </td><td><input type="text" name="namelast" size=20 maxlength=30><font size=-1 color=red>* required</td></tr>
<tr>
<td><font face=arial size=2>Work</td><td><input type="text" name="work" size=20 maxlength=15></td></tr>
<tr>
<td><font face=arial size=2>Home</td><td><input type="text" name="home" size=15></td></tr>
<tr>
<td><font face=arial size=2>Cell</td><td><input type="text" name="cell" size=15></td></tr>
<tr>
<td><font face=arial size=2>eMail  </td><td><input type="text" name="email" size=20 maxlength=40> <font size=-1 color=red>* required</td></tr>
 
<tr>
<td><font face=arial size=2>Comments<br>
<font size=1> for example: building services offered </td><td><input type="text" name="comments" size=85 maxlength=85></td></tr>
 

<tr><td><br><br><br> <input type="SUBMIT" value="Add your details"></td><td></td></tr></table>

<?php
   } else {
      error_reporting(0);
      // initialize a array to 
      //hold any errors we encounter
      $errors = array();

     
 //$datechg = curdate();
//$datechg = $_POST['date'];
$datechg = date("Y-m-d");
$jpg = "aaa.jpg";

      // test to see if the form was actually 
      // posted from our form
      // In testing, if you get an Inavlid referer error
      // comment out or remove the next three lines
      $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
      if (!ereg($page, $_SERVER['HTTP_REFERER']))
         $errors[] = "Invalid referer";

      // check to see if a name and valid email was entered
	
       	 if (!$_POST['namefirst'])
	         $errors[] = "Error-Msg(203) Your first name is required";
 	 if (!$_POST['namelast'])
	         $errors[] = "Error-Msg(204) Your last name is required";
	if (!$_POST['email'])
        	 $errors[] = "Error-Msg(205) No Email Address detected - required";
	 
      // if there are any errors, display them
      if (count($errors)>0) {
         foreach($errors as $err)
            echo "<center><br>$err<br>\n";
         echo "<br>Please use your browser's Back button to fix.";
      } else {
         // no errors, so we build our message
          
          
    

//  above is validation stuff
//  following is code to update 411 file
//



//  validate entries

  

$sql_result = mysql_query($sql, $dbh) or die ("zero-connection to Player info  file");

if (!sql_result) {
echo "<p> Could not add record to file";
} 
else 
{
echo "
<p>Your Contact Details have been added.</p>
<p>Wolfhounds 411 is not visible to the public so your details are reasonably secure.</P>
<center>
$datechg
<table border=1 cellpadding=10 width=500>
<tr><td width=20%><font face=arial><b>    Name</td><td width=80%> <font face=arial>$namefirst  $namelast <hr size=1></td></tr>
<tr><td><font face=arial><b>Home #</td><td><font face=arial>$home</td></tr>
<tr><td><font face=arial><b>Work #</td><td><font face=arial>$work</td></tr>
<tr><td><font face=arial><b>Cell #</td><td><font face=arial>$cell</td></tr>
<tr><td><font face=arial><b>Email</td><td><font face=arial>$email</td></tr>
<tr><td><font face=arial><b>Comments</td><td><font face=arial>$comments</td></tr>
</table><br><br>
<a href='./test_update.php'>Back</a>";
 }

}
}


?>
</body>
</html>

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]

Posted: Fri Aug 18, 2006 6:57 pm
by feyd
If you echo $sql immediately before mysql_query() is called, what does it output?

Posted: Fri Aug 18, 2006 8:10 pm
by Shendemiar
Dont use date-fields, they give headaches. Use int-field and store unix timestamps.

echo mysql _query result

Posted: Fri Aug 18, 2006 8:39 pm
by Sid Rosenburg
INSERT INTO players ( namefirst, namelast, work, home, cell, email, jpg, comments, datechg ) values ('atestname1', 'atestname2', 'awork#', 'ahome#', 'acell#', 'abc@abc.com' , '', 'test comment', '')


where axxx etc were fields entered into the form.

there is nothing ing the jpg field expected and nothing in the datechg unexpected

Posted: Fri Aug 18, 2006 10:34 pm
by feyd
..and I see why too. You've created the insertion string far too early. Early enough that when you set $datechg, it's long long too late. Move the setting of $sql after $datechg is done.

And thats why they call - you the great one -

Posted: Fri Aug 18, 2006 11:03 pm
by Sid Rosenburg
Thank you sir,

Your suggestion worked.

Thank you for your patience and enjoy Memphis. In 2004 I visited for a weekend

Beale St C-r-a-z-y Bananas.

Not for me though, much prefer cold winters and snow.

Again Thank you.