Status posting error

Ask about a PHP problem here.
Post Reply
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Status posting error

Post by Z645 »

So i tried creating some code of my own and I can't get the status placed into the MySQL database. Also, it shows no errors when I use error_reporting();

home.php
[syntax=php]<?php

include('core/init.inc.php');

$errors = array();

if (isset($_POST['status'])){
if(empty($_POST['status'])){
$errors[] = 'Your status may not be empty!';
}

if (empty($errors)){
get_feeds($_POST['status']);

header('Location: home.php');
echo 'Status Successfully posted!';
die();
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome to Socialcrunch</title>
<meta name="description" content="Welcome to Socialcrunch. You may connect with friends, meet new ones, and also hang out when you have free time!" />
<meta name="keywords" content="social, networking, games, chat, hangout, friends" />
<meta name="robots" content="index, follow" />

<link rel="stylesheet" href="styles/style.css" type="text/css">
</head>

<body>

<div id="wrapper">
<?php
mysql_connect('mysql6.000webhost.com','username','password');
mysql_select_db('database');
?>
<div id="navigation">
<ul>
<li><a href="">Settings</a></li>
<li><a href=""><img src=""></img><?php $user_info['username']; ?></a></li>
<li><a href="http://"><img src=""></img></a></li>
</ul>
</div>
<div id="main">
<div id="update">
<div>
<?php

if (empty($errors) === false){
?>
<ul>
<?php

foreach ($errors as $error){
echo "<li>{$error}</li>";
}

?>
</ul>
<?php
}

?>
</div>
<form action="" method="post">
<p class="status">
<textarea name="status" id="status" cols="50" rows="5" placeholder="Update your life here..."></textarea>
</p>
<input type="submit" value="Post">
</form>
</div>
<h1>What others have posted</h1>
<?php include('feeds.php'); ?>
</div>
</div>
<div id="footer">
<p>&copy; SocialCrunch 2013. All Rights Reserved.</p>
</div>
</body>
</html>[/syntax]

feeds.php
[syntax=php] <div class="post">
<img src="" alt="" />
<div class="content">
<?php
include('core/inc/user.inc.php');
mysql_connect('mysql6.000webhost.com','username','password');
mysql_select_db('database');
$query = mysql_query('SELECT * FROM feeds ORDER BY id DESC');
while($output = mysql_fetch_assoc($query)){
$numberComments = mysql_query("SELECT id FROM newscomments WHERE id = '".$output['id']."'");
echo $output['username'].'<br />';
echo $output['status'].'<br />';
echo '<a href="viewcomments.php?id='.$output['id'].'">View Comments</a> <hr />';
}
?>
</div>
</div>[/syntax]

user.inc.php
[syntax=php]<?php

// Checks if the given username exists in the table.
function user_exists($user){
$user = mysql_real_escape_string($user);

$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'");

return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks if the given username and password combination is valid.
function valid_credentials($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = mysql_real_escape_string($pass);

$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}' AND `user_password` = '{$pass}'");

return (mysql_result($total, 0) == '1') ? true : false;
}


// Checks if the given user account is active
function is_active($user){
$user = mysql_real_escape_string($user);

$sql = "SELECT
COUNT(`user_activations`.`user_id`)
FROM `users`
INNER JOIN `user_activations`
ON `users`.`user_id` = `user_activations`.`user_id`
WHERE `users`.`user_name` = '{$user}'";

$result = mysql_query($sql);

return (mysql_result($result, 0) == '0') ? true : false;
}

// Activates the account related to the given activation code
function activate_account($aid){
$aid = mysql_real_escape_string($aid);

mysql_query("DELETE FROM `user_activations` WHERE `activation_code` = '{$aid}'");
}

// Adds a user to the Database.
function add_user($user, $email, $pass, $first_name, $last_name){
$user = mysql_real_escape_string(htmlentities($user));
$email = mysql_real_escape_string($email);
$pass = sha1($pass);
$first_name = mysql_real_escape_string(htmlentities($first_name));
$last_name = mysql_real_escape_string(htmlentities($last_name));

$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
$aid = implode('', array_rand($charset, 10));

$body = <<<EMAIL

Welcome to Socialcrunch!

Before you can login you must activate your account by clicking the link below.

http://socialcrunch.comyr.com/activate.php?aid={$aid}

EMAIL;

mail($email, 'Welcome to Socialcrunch', $body, 'From: admin@socialcrunch.comyr.com');

mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`, `first_name`, `last_name`) VALUES ('{$user}', '{$pass}', '{$email}', '{$first_name}', '{$last_name}')");

$user_id = mysql_insert_id();

mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')");
}

// Adds feeds to the Feeds Database
function get_feeds($id, $user, $time, $status){
$user = mysql_real_escape_string(htmlentities($user));
$status = mysql_real_escape_string(htmlentities($status));

$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
$id = implode('', array_rand($charset, 6));

mysql_query("INSERT INTO `feeds` (`id`, `username`, `time`, `status`) VALUES (`{$id}`, `{$user}`, `{$time}`, `{$status}`)");
}

// Displays the feeds on the users page
function give_feeds(){

}
?>[/syntax]

or see it on the website: socialcrunch.comyr.com
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Status posting error

Post by ExtremeGaming »

[syntax=php]mysql_query("INSERT INTO `feeds` (`id`, `username`, `time`, `status`) VALUES (`{$id}`, `{$user}`, `{$time}`, `{$status}`)");[/syntax]

You're using backticks in your VALUES clause. Change those to apostrophes
<?php while(!$succeed = try()); ?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Status posting error

Post by Temor »

If you have reason to believe that your SQL is wacky you should use mysql_error();

[syntax=php]echo mysql_error();[/syntax]
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: Status posting error

Post by Z645 »

Thanks guys, now to get working on giving that data back to the user :/
Post Reply