SauloA Posted January 24, 2007 Share Posted January 24, 2007 I created a form to insert a record. There is a a text box that is supposed to submit the date of when record is posted. I'm trying to submit a value of Now() to the database but my datetime field in my MySQL database stored '0000-00-00 00:00:00'. The problem seems to be the value of Now().Here's my code. The red is where I think the problem lies. I'd appreciate any help.<body><h1>Admin Home Page</h1><p>Welcome <?php echo $_SESSION['MM_Username']; ?> </p><p><a href="logout.php">Logout</a></p><form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1"> <table> <caption>Insert New Review</caption> <tr valign="baseline"> <td nowrap="nowrap" align="right">Review Date:</td> [color=red]<td><input type="text" name="urev_date" value="Now()" size="32" /></td>[/color] </tr> Quote Link to comment Share on other sites More sharing options...
anatak Posted January 24, 2007 Share Posted January 24, 2007 Can you post your update or insert statement for the SQL ?Now() is a mysql function and not a php function.so your problem is most likely in the query you are trying to buildanatak Quote Link to comment Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 Here's the insertion code I'm using. I pasted all my code just in case.<?php require_once('../Connections/connBlog.php'); ?><?php require_once('../Connections/connBlog.php'); ?><?phpif (!isset($_SESSION)) { session_start();}$MM_authorizedUsers = "1,2";$MM_donotCheckaccess = "false";// *** Restrict Access To Page: Grant or deny access to this pagefunction isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { // For security, start by assuming the visitor is NOT authorized. $isValid = False; // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. // Therefore, we know that a user is NOT logged in if that Session variable is blank. if (!empty($UserName)) { // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. // Parse the strings into arrays. $arrUsers = Explode(",", $strUsers); $arrGroups = Explode(",", $strGroups); if (in_array($UserName, $arrUsers)) { $isValid = true; } // Or, you may restrict access to only certain users based on their username. if (in_array($UserGroup, $arrGroups)) { $isValid = true; } if (($strUsers == "") && false) { $isValid = true; } } return $isValid; }$MM_restrictGoTo = "default.php";if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { $MM_qsChar = "?"; $MM_referrer = $_SERVER['PHP_SELF']; if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&"; if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) $MM_referrer .= "?" . $QUERY_STRING; $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer); header("Location: ". $MM_restrictGoTo); exit;}?><?phpfunction GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO user_review_tbl (urev_date, shop_id, urev_approved, user_id, urev_review) VALUES (%s, %s, %s, %s, %s)", GetSQLValueString($_POST['urev_date'], "text"), GetSQLValueString($_POST['shop_id'], "int"), GetSQLValueString(isset($_POST['urev_approved']) ? "true" : "", "defined","1","0"), GetSQLValueString($_POST['user_id'], "int"), GetSQLValueString($_POST['urev_review'], "text")); mysql_select_db($database_connBlog, $connBlog); $Result1 = mysql_query($insertSQL, $connBlog) or die(mysql_error()); $insertGoTo = "home.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));}mysql_select_db($database_connBlog, $connBlog);$query_rsShop = "SELECT shop_id, shop_name FROM shop_tbl ORDER BY shop_name ASC";$rsShop = mysql_query($query_rsShop, $connBlog) or die(mysql_error());$row_rsShop = mysql_fetch_assoc($rsShop);$totalRows_rsShop = mysql_num_rows($rsShop);$maxRows_rsReviews = 10;$pageNum_rsReviews = 0;if (isset($_GET['pageNum_rsReviews'])) { $pageNum_rsReviews = $_GET['pageNum_rsReviews'];}$startRow_rsReviews = $pageNum_rsReviews * $maxRows_rsReviews;mysql_select_db($database_connBlog, $connBlog);$query_rsReviews = "SELECT urev_id, urev_date, urev_approved, shop_id FROM user_review_tbl ORDER BY urev_date DESC";$query_limit_rsReviews = sprintf("%s LIMIT %d, %d", $query_rsReviews, $startRow_rsReviews, $maxRows_rsReviews);$rsReviews = mysql_query($query_limit_rsReviews, $connBlog) or die(mysql_error());$row_rsReviews = mysql_fetch_assoc($rsReviews);if (isset($_GET['totalRows_rsReviews'])) { $totalRows_rsReviews = $_GET['totalRows_rsReviews'];} else { $all_rsReviews = mysql_query($query_rsReviews); $totalRows_rsReviews = mysql_num_rows($all_rsReviews);}$totalPages_rsReviews = ceil($totalRows_rsReviews/$maxRows_rsReviews)-1;?> Quote Link to comment Share on other sites More sharing options...
anatak Posted January 24, 2007 Share Posted January 24, 2007 $insertSQL = sprintf("INSERT INTO user_review_tbl (urev_date, shop_id, urev_approved, user_id, urev_review) VALUES (%s, %s, %s, %s, %s)", GetSQLValueString($_POST['urev_date'], "text"), GetSQLValueString($_POST['shop_id'], "int"), GetSQLValueString(isset($_POST['urev_approved']) ? "true" : "", "defined","1","0"), GetSQLValueString($_POST['user_id'], "int"), GetSQLValueString($_POST['urev_review'], "text"));I don't think you can insert Now() like that.I think but I am not sure you have to insert a Now() value likeINSERT INTO table (urev_date)SELECT Now();You can help yourself by printing the query to your screen to have a look at it.echo $insertSQL Quote Link to comment Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 I'm still kinda new to PHP so can you elaborate how I would do what you suggest?Where would I put the coding you suggest? Quote Link to comment Share on other sites More sharing options...
anatak Posted January 26, 2007 Share Posted January 26, 2007 I am sorry I am not sure myself how to do this.I always use datetime fields to get the current time of an update.an update of a row (or insert of a new ros) will automatically update or insert the current time into the first date time field in a table (NOTE the FIRST).I suggest you ask the moderator to move this question to mysql help.or look for a php function to do this.in php I would do something like$date = date() look at the date() function and make sure you format it correctly for the mysql format (YYYY-MM-DD if I am not mistaken)you have to chose what you want to do php or mysql your choice is dependant on what you want to do with the date field.for the mysql option it would be something like $insertSQL = sprintf("INSERT INTO user_review_tbl (urev_date, shop_id, urev_approved, user_id, urev_review) VALUES (%s, %s, %s, %s, %s)", SELECT Now(), GetSQLValueString($_POST['shop_id'], "int"), GetSQLValueString(isset($_POST['urev_approved']) ? "true" : "", "defined","1","0"), GetSQLValueString($_POST['user_id'], "int"), GetSQLValueString($_POST['urev_review'], "text"));disclaimer.I did not have time to test this but I think this is more or less how to do it.I don't use a lot of insert select statements myself so I might be hugely mistake in SQL syntaxanatak Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.