Jump to content

[SOLVED] Inserting user selected time value


GreenUser

Recommended Posts

Hi all!

 

My $_POST['when'] is not doing what I want.  How can I rename it?  It is taking a user submitted javascript calender date, which is set to be in mysql format.  The code worked fine until I added this.  I tried using

DATE(),  and  DATE() 'when' ,''  and 'DATE: Manual Date', 'when'

each on its own, to substitute but that didn't work. 

 

<?php
if(isset($_POST['submit'])) {
$sql = "INSERT INTO stories(cat_id, username, dateposted, location, when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est)
	VALUES('".$_POST['cat']."', '".$_SESSION['SESS_USERNAME']."', NOW(), '".$_POST['location']."', 
'".$_POST['when']."',         <--- the problem
	'".$_POST['datename']."', '".$_POST['body']."',
	".$_POST['fri'].", ".$_POST['fun'].", ".$_POST['soh'].", ".$_POST['con'].", ".$_POST['sma'].",
	".$_POST['sex'].", ".$_POST['hyg'].", ".$_POST['mes'].", ".$_POST['aut'].", ".$_POST['est'].")";
?>

 

I can post the rest of the script but after mysql error and php error results I have narrowed it down to this.

 

Thanks!

Link to comment
Share on other sites

Datatype for 'when' column is simply Date. 

 

Value format is yyyy-mm-dd.

 

<tr>
<td>When</td>
<td><input type="text" name="when"><a href="javascript:cal9.popup();"><img src="img/cal.gif"></a></td>
</tr>

 

Sorry to have left that out.  Mysql Server V. is 5.0 and PHP is 5.2.1

Link to comment
Share on other sites

Results are

INSERT INTO stories(cat_id, username, dateposted, location, when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est) VALUES('1', 'John', NOW(), 'Alabama', 2008-06-15, 'adsfadsfadsfdsf', 'adsfadsfadsfadsfadsfadsfadsf', 7, 7, 7, 7, 7, 7, 7, 7, 7, 7)

 

I thought I could replace

'".$_POST['when']."', 

with

'DATE: Manual Date', 'when'

 

Not sure how to set this one up.  ???

 

Link to comment
Share on other sites

Here it gets

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est) VALUE' at line 1

 

... So, that narrows it down to the 'when' value, right?

 

 

Link to comment
Share on other sites

<?php

session_start();

require("config.php");
require("db.php");

if(isset($_POST['submit'])) {
$sql = "INSERT INTO stories(cat_id, username, dateposted, location, when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est)
	VALUES('".$_POST['cat']."', '".$_SESSION['SESS_USERNAME']."', NOW(), '".$_POST['location']."',
	".$_POST['when'].",
	'".$_POST['datename']."', '".$_POST['body']."',
	".$_POST['fri'].", ".$_POST['fun'].", ".$_POST['soh'].", ".$_POST['con'].", ".$_POST['sma'].",
	".$_POST['sex'].", ".$_POST['hyg'].", ".$_POST['mes'].", ".$_POST['aut'].", ".$_POST['est'].")";

	mysql_query($sql);

header("Location: " . $config_basedir . "/index.php");
}
	else {
require("header.php");
?>

<div id=main1>
<form name="addstory" action="<?php echo $SCRIPT_NAME ?>" method="post">

<table>
<tr>
<td colspan='2'>Enter form.</td>
</tr>

<tr>
<td>This was a</td>
<td>
<select name="cat">
<?php
	$catsql = "SELECT * FROM categories;";
	$catres = mysql_query($catsql);
	while($catrow= mysql_fetch_assoc($catres)) {
		echo "<option value='" . $catrow['id'] . "'>" . $catrow['category'] . "</option>";
	}
?>
</select>
</td>
</tr>

<tr>
<td>Location</td>
<td><select name="location">
<option value ="Alabama">Alabama</option>
  	<option value ="California">California</option>
  	<option value ="Colorado" selected="selected">Colorado</option>
  	<option value ="New Hampshire">New Hampshire</option>
  	<option value ="Massachusetts">Massachusetts</option>
<option value ="Montana">Montana</option>
</select></td>

</tr>
////////////////////////////
<tr>
<td>When</td>
<td><input type="text" name="when"><a href="javascript:cal9.popup();"><img src="img/cal.gif"></a></td>
</tr>
///////////////////////////
<tr>
<td>Name (Of Your Date)</td>
<td><input type="text" name="datename"></td>
</tr><tr>
<td>Rate Your Date</td>
<td><textarea name="body" rows="10" cols="50"></textarea></td>
</tr><tr>
<td colspan='2'>rate these qualities from 1 to 10 (10 being ideal).</td>
</tr>
<tr>
<td>fri</td>
<td>
<input type="radio"  name="fri" value=1>1</option>
  	<input type="radio"  name="fri" value ="2">2</option>
  	<input type="radio"  name="fri" value ="3">3</option>
  	<input type="radio"  name="fri" value ="4">4</option>
  	<input type="radio"  name="fri" value ="5">5</option>
  	<input type="radio"  name="fri" value ="6">6</option>
  	<input type="radio"  name="fri" value ="7">7</option>
  	<input type="radio"  name="fri" value ="8">8</option>
  	<input type="radio"  name="fri" value ="9">9</option>
  	<input type="radio"  name="fri" value ="10">10</option>
</td></tr><tr>
<td>Fun</td>
<td>
<input type="radio"  name="fun" value=1>1</option>
  	<input type="radio"  name="fun" value ="2">2</option>
  	<input type="radio"  name="fun" value ="3">3</option>
  	<input type="radio"  name="fun" value ="4">4</option>
  	<input type="radio"  name="fun" value ="5">5</option>
  	<input type="radio"  name="fun" value ="6">6</option>
  	<input type="radio"  name="fun" value ="7">7</option>
  	<input type="radio"  name="fun" value ="8">8</option>
  	<input type="radio"  name="fun" value ="9">9</option>
  	<input type="radio"  name="fun" value ="10">10</option>
</td></tr><tr>
<td>soh</td>
<td>
<input type="radio" name="soh" value ="1">1</option>
  	<input type="radio" name="soh" value ="2">2</option>
  	<input type="radio" name="soh" value ="3">3</option>
  	<input type="radio" name="soh" value ="4">4</option>
  	<input type="radio" name="soh" value ="5">5</option>
  	<input type="radio" name="soh" value ="6">6</option>
  	<input type="radio" name="soh" value ="7">7</option>
  	<input type="radio" name="soh" value ="8">8</option>
  	<input type="radio" name="soh" value ="9">9</option>
  	<input type="radio" name="soh" value ="10">10</option>
</td></tr><tr>
<td>con</td>
<td>
<input type="radio" name="con" value ="1">1</option>
  	<input type="radio" name="con" value ="2">2</option>
  	<input type="radio" name="con" value ="3">3</option>
  	<input type="radio" name="con" value ="4">4</option>
  	<input type="radio" name="con" value ="5">5</option>
  	<input type="radio" name="con" value ="6">6</option>
  	<input type="radio" name="con" value ="7">7</option>
  	<input type="radio" name="con" value ="8">8</option>
  	<input type="radio" name="con" value ="9">9</option>
  	<input type="radio" name="con" value ="10">10</option>
</td></tr><tr>
<td>sma</td>
<td>
<input type="radio" name="sma" value ="1">1</option>
  	<input type="radio" name="sma" value ="2">2</option>
  	<input type="radio" name="sma" value ="3">3</option>
  	<input type="radio" name="sma" value ="4">4</option>
  	<input type="radio" name="sma" value ="5">5</option>
  	<input type="radio" name="sma" value ="6">6</option>
  	<input type="radio" name="sma" value ="7">7</option>
  	<input type="radio" name="sma" value ="8">8</option>
  	<input type="radio" name="sma" value ="9">9</option>
  	<input type="radio" name="sma" value ="10">10</option>
</td></tr><tr>
<td>sex</td>
<td>
<input type="radio" name="sex" value ="1">1</option>
  	<input type="radio" name="sex" value ="2">2</option>
  	<input type="radio" name="sex" value ="3">3</option>
  	<input type="radio" name="sex" value ="4">4</option>
  	<input type="radio" name="sex" value ="5">5</option>
  	<input type="radio" name="sex" value ="6">6</option>
  	<input type="radio" name="sex" value ="7">7</option>
  	<input type="radio" name="sex" value ="8">8</option>
  	<input type="radio" name="sex" value ="9">9</option>
  	<input type="radio" name="sex" value ="10">10</option>
</td></tr><tr>
<td>hyg</td>
<td>
<input type="radio" name="hyg" value ="1">1</option>
  	<input type="radio" name="hyg" value ="2">2</option>
  	<input type="radio" name="hyg" value ="3">3</option>
  	<input type="radio" name="hyg" value ="4">4</option>
  	<input type="radio" name="hyg" value ="5">5</option>
  	<input type="radio" name="hyg" value ="6">6</option>
  	<input type="radio" name="hyg" value ="7">7</option>
  	<input type="radio" name="hyg" value ="8">8</option>
  	<input type="radio" name="hyg" value ="9">9</option>
  	<input type="radio" name="hyg" value ="10">10</option>
</td></tr><tr>
<td>Mes</td>
<td>
<input type="radio" src="img/full.gif" name="mes" value ="1">1</option>
  	<input type="radio" name="mes" value ="2">2</option>
  	<input type="radio" name="mes" value ="3">3</option>
  	<input type="radio" name="mes" value ="4">4</option>
  	<input type="radio" name="mes" value ="5">5</option>
  	<input type="radio" name="mes" value ="6">6</option>
  	<input type="radio" name="mes" value ="7">7</option>
  	<input type="radio" name="mes" value ="8">8</option>
  	<input type="radio" name="mes" value ="9">9</option>
  	<input type="radio" name="mes" value ="10">10</option>
</td></tr><tr>
<td>aut</td>
<td>
<input type="radio" name="aut" value ="1">1</option>
  	<input type="radio" name="aut" value ="2">2</option>
  	<input type="radio" name="aut" value ="3">3</option>
  	<input type="radio" name="aut" value ="4">4</option>
  	<input type="radio" name="aut" value ="5">5</option>
  	<input type="radio" name="aut" value ="6">6</option>
  	<input type="radio" name="aut" value ="7">7</option>
  	<input type="radio" name="aut" value ="8">8</option>
  	<input type="radio" name="aut" value ="9">9</option>
  	<input type="radio" name="aut" value ="10">10</option>
</td>
</tr><tr>
<td>est</td>
<td>
<input type="radio" name="est" value ="1">1</option>
  	<input type="radio" name="est" value ="2">2</option>
  	<input type="radio" name="est" value ="3">3</option>
  	<input type="radio" name="est" value ="4">4</option>
  	<input type="radio" name="est" value ="5">5</option>
  	<input type="radio" name="est" value ="6">6</option>
  	<input type="radio" name="est" value ="7">7</option>
  	<input type="radio" name="est" value ="8">8</option>
  	<input type="radio" name="est" value ="9">9</option>
  	<input type="radio" name="est" value ="10">10</option>
</td>
</tr><tr>
<td></td>
<td><input type="submit" name="submit" value="Post Your Date!"></td>
</tr>
</table>
</form>

<script language="JavaScript">
var cal9 = new calendar3(document.forms['addstory'].elements['when']);
cal9.year_scroll = true;
cal9.time_comp = false;
</script>

</div>
<br><br>
<?php
}
require("footer.php");
?>

Link to comment
Share on other sites

try this:

$cat_id=mysql_real_escape_string($_POST['cat']);
$user_in_session=$_SESSION['SESS_USERNAME'];
$now=date('Y-m-d H:i:s');
$location=mysql_real_escape_string($_POST['location']);
$when=mysql_real_escape_string($_POST['when']);
$datename=mysql_real_escape_string($_POST['datename']);
$body=mysql_real_escape_string($_POST['body']);
$fri=mysql_real_escape_string($_POST['fri']);
$fun=mysql_real_escape_string($_POST['fun']);
$soh=mysql_real_escape_string($_POST['soh']);
$con=mysql_real_escape_string($_POST['con']);
$sma=mysql_real_escape_string($_POST['sma']);
$sex=mysql_real_escape_string($_POST['sex']);
$hyg=mysql_real_escape_string($_POST['hyg']);
$mes=mysql_real_escape_string($_POST['mes']);
$aut=mysql_real_escape_string($_POST['aut']);
$est=mysql_real_escape_string($_POST['est']);
if(isset($_POST['submit'])) {
$sql = "INSERT INTO stories(cat_id, username, dateposted, location, when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est)
	VALUES('$cat_id', '$user_in_session', $now, '$location','$when','$datename', '$body','$fri','$fun','$soh','$con','$sma','$sex','$hyg'
	,'$mes','$aut','$est')";

	mysql_query($sql) or die(mysql_error());

header("Location: " . $config_basedir . "/index.php");
}

Link to comment
Share on other sites

Darn!  Same error message that looked good too. 

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est) VALUE' at line 1

 

Thanks for your help, I need to go to bed now.  I'll report back with any new finds.  Thanks again.  :)

Link to comment
Share on other sites

The date needs single quotes around it:

 

$sql = "INSERT INTO stories(cat_id, username, dateposted, location, when, datename, body, fri, fun, soh, con, sma, sex, hyg, mes, aut, est)
	VALUES('".$_POST['cat']."', '".$_SESSION['SESS_USERNAME']."', NOW(), '".$_POST['location']."',
	'".$_POST['when']."',
	'".$_POST['datename']."', '".$_POST['body']."',
	".$_POST['fri'].", ".$_POST['fun'].", ".$_POST['soh'].", ".$_POST['con'].", ".$_POST['sma'].",
	".$_POST['sex'].", ".$_POST['hyg'].", ".$_POST['mes'].", ".$_POST['aut'].", ".$_POST['est'].")";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.