Jump to content

Help with INSERT into mysql


firecat318

Recommended Posts

When people join my website, they choose a username, password, email and a couple other things. 

I want to have it though, so that later they can fill in some other information to their profile.

One of these things is their favorite quote.

 

Here is the profile.php page

 

<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
?>

<html><head><TITLE>Page</TITLE>
<link rel="stylesheet" type="text/css" href="../style.css" />
<meta name="description" content="STUFF" />
</head>

<body bgcolor="#E5E5E5">

<?	
if($session) {

$username = mysql_real_escape_string($_GET['user']);

$query = "SELECT * FROM `users` WHERE `username` = '$session'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$sql = mysql_query("SELECT `quote` FROM `users` WHERE `username` = '$session'");
$quote = mysql_result($sql, 0);

?>

<div id="header">
<table><tr><td align="left"><table>
	<tr><td width="220px" height="26" id="welcome">
	<a href="/"><img src="../images/logo.gif"></a></td></tr> 
</table>
	</td><td align="center">
	<table>
	<tr><td align="center"><a href="features.php" id="top_links" class="top">features</a></td><td><b>|</b></td>
	<td align="center"><a href="forum" id="top_links" class="top">forums</a></td><td><b>|</b></td>

	<?
	if($session) {
	$query = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$session'");
	$row = mysql_fetch_assoc($query); ?>



	<?
	if($session) {
	?>

	<td align="right">
	<a href="logout.php" class="forgot">Logout</a>
	</td>

	<?
	} 
	?>

</table>	
</div> <br />
<?
if($session) {
include("header.php");
}
?>

<div id="body">
<table width="100%"><tr><td align="left">
<div id="nice" width="200px">
Favorite Quote: <? echo "$quote"; ?>
</div>
</td></tr></table>
</div>

 

(I left some parts out, but that is the main stuff.)

 

Now for the edit.php file, where the user edits his/her profile information.

 

<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
$query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'");
$id = mysql_result($query, 0);
echo "$id";
?>

<b>Edit your profile:</b><br><br><br>
<font color=red>Favorite Quote:</font>
<form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST">
<textarea cols="40" rows="10" name="favorites">
</textarea><br>
<input type="submit" name="quote" value="Update">
</form>

<?

$sql = mysql_query("INSERT INTO `users` (quote) VALUES('$quote') WHERE `username` = '$session'");
echo "Thank you for updating your profile!";
if(!$sql) {
 die mysql_error();
}
?>		

 

$session is equal to their username, which is logged when they log in.  I use this to identify the user when I have to get stuff from the mysql database, but on the edit.php page, when I try to put their quote into their column of information, nothing happens. 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/103196-help-with-insert-into-mysql/
Share on other sites

You SQL query is incorrect. You need to UPDATE rather then insert

 

Also try escaping the quote before putting it in the query.

 

 

 

eg:

 

<?php

$escaped_quote = mysql_real_escape_string($quote);
$sql = mysql_query("UPDATE `users` SET quote ='{$escaped_quote}'  WHERE `username` = '{$session}'");
echo "Thank you for updating your profile!";
if(!$sql) {
 die mysql_error();
}?>

 

Also are you getting any error messages?

Alright, well I fixed the Syntax a little bit, because I got a mysql error, but it still doesn't work.  It says it works, but nothing gets inserted into the mysql database. 

 

here is my new code

 

<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
$query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'");
$id = mysql_result($query, 0);
echo "$id";
?>

<b>Edit your profile:</b><br><br><br>
<font color=red>Favorite Quote:</font>
<form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST">
<textarea cols="40" rows="10" name="favorites">
</textarea><br>
<input type="submit" name="quote" value="Update">
</form>

<?

$quote = $_POST['favorites'];

$sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = 'session'");
echo "Thank you for updating your profile!";
if(!$sql) {
 die(mysql_error());
}
?>		

<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
$query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'");
$id = mysql_result($query, 0);
echo "$id";
?>

<b>Edit your profile:</b><br><br><br>
<font color=red>Favorite Quote:</font>
<form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST">
<textarea cols="40" rows="10" name="favorites">
</textarea><br>
<input type="submit" name="quote" value="Update">
</form>

<?

$quote = $_POST['favorites'];

$sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = '$session'");
echo "Thank you for updating your profile!";
if(!$sql) {
 die(mysql_error());
}
?>

Actually I forgot a little bit of the code lol

 

<?
session_start();
$session = $_SESSION['username'];
include("connection.php");
$query = mysql_query("SELECT `id` FROM `users` WHERE `username` = '$session'");
$id = mysql_result($query, 0);
echo "$id";
?>

<b>Edit your profile:</b><br><br><br>
<font color=red>Favorite Quote:</font>
<form action=" <? $_SERVER['PHP_SELF']; ?> " method="POST">
<textarea cols="40" rows="10" name="favorites">
</textarea><br>
<input type="submit" name="quote" value="Update">
</form>

<?

$quote = $_POST['favorites'];

$sql = mysql_query("UPDATE `users` SET `quote` = '$quote' WHERE `username` = 'session'");
echo "Thank you for updating your profile!";
if(!$sql) {
 die(mysql_error());
}
?>		

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.