Reputation: 569
Working out a simple star ratings script. Data not inserting into SQL. I am passing a 'id' in the url from the form action to coincide with the rating. I'm using GET to retreive that value. I'm using POST to insert the radio button value. I've tested and the url is passing the id. The script runs to completion. SQL is connecting.
index.html
<form method="POST" action="rating.php?id=<?php echo $id; ?>">
<fieldset class="rating">
<legend>. . .</legend>
<input type="radio" id="star3" name="starno" value="3" onclick="this.form.submit()"/>
<label for="star3" title="Meh">3 stars</label>
<input type="radio" id="star2" name="starno" value="2" onclick="this.form.submit()"/>
<label for="star2" title="Kinda bad">2 stars</label>
<input type="radio" id="star1" name="starno" value="1" onclick="this.form.submit()"/>
<label for="star1" title="Sucks big time">1 star</label>
</fieldset>
</form>
ratings.php
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
isset($_GET['id']);
$storyidr = $_GET['id'];
$star = $_POST['starno'];
$con=mysqli_connect ("","","","");
$sql = 'INSERT INTO ratings (storyidr,rank,entry_date)';
$sql .= 'VALUES("'.$storyidr.'","'.$star.'",now())';
mysqli_close($con);
print_r(array_keys(get_defined_vars()));
print_r(array_values(get_defined_vars()));
?>
SQL
CREATE TABLE IF NOT EXISTS `ratings`
(
`ratingid` int(11) NOT NULL AUTO_INCREMENT,
`storyidr` varchar(11) NOT NULL,
`rank` int(11) NOT NULL,
`entry_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`ratingid`),
UNIQUE KEY `ratingid` (`ratingid`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='rating for stories' AUTO_INCREMENT=1 ;
error report
( [affected_rows] => [client_info] => [client_version] => 50532 [connect_errno] => 0 [connect_error] => [errno] => [error] => [error_list] => [field_count] => [host_info] => [info] => [insert_id] => [server_info] => [server_version] => [stat] => [sqlstate] => [protocol_version] => [thread_id] => [warning_count] => ) [7] => INSERT INTO ratings (storyidr,rank,entry_date)VALUES("198","3",now()) )
Upvotes: 0
Views: 1024
Reputation: 499
If you are connected to DB where is rating table, table exist, php work fine, id is passing, mysqli_connect is connected then maybe a space before VALUES like this:
$sql .= ' VALUES("'.$storyidr.'","'.$star.'",now())';
as i'm aware code which U have right now is not valid
INSERT INTO ratings (storyidr,rank,entry_date)VALUES("'.$storyidr.'","'.$star.'",now())
/// edited to add some security ;)
Use PDO to handle with MySQL all time:
try {
$pdo = new PDO('mysql:host=local;dbname=db','user','pass');
$pdo->setAsttribute(PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO ratings SET name = :name, surname = :surname';
$oo_db = $pdo->prepare($sql);
$oo_db->bindValue(':surname', $surname);
$oo_db->execute();
}
catch (PDOException $e) { $error = "fail"; }
more at -> http://www.phpeveryday.com/articles/PDO-Positional-and-Named-Placeholders-P551.html
Upvotes: 1