Jump to content

Need some help with PHP and MySQL


Chiaki

Recommended Posts

I've been tinkering with it all day, but the thing just refuses to do what I want. I'm trying to make it so that when a mysql_query is sent, it reads the table and exports the required information. Basically, I have a table called "accounts" with the fields of "user" and "email." I want the script to call up the user field and output everything thats in it. Here's what my code looks like:
[code]
<?php
include ("dbconnect.php");
$query="SELECT user FROM accounts";
$result=mysql_query($query);
$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"user");
echo "<b>$name</b>";

$i++;
}
?>[/code]
Link to comment
Share on other sites

Okay, I have another problem now, related to something a little different. I found out how to do the things I asked above. (thanks, by the way!)

This time, I'm having problem submitting stuff INTO the database tables. I'm not even sure what I'm doing wrong. Here is the code:

profile.php
[code]
<form action="addprofile.php" method="post">
<b>Profile:</b><BR><textarea name="info" rows="5" cols="50"></textarea>
<br/>
<br/>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>
[/code]

addprofile.php <-This one adds an entirely new field with NULL, NULL, NULL, NULL, and then what the user entered in its things. To access to above submitting form, the person has to log in first and then it opens up on the same page. How do I make it so it uses their user-name to tell were to insert the info field.
[code]<?
$info = $_POST["info"];
include("dbconnect.php");
$profile=MYSQL_QUERY("INSERT INTO account (id,user,password,email,info)". "VALUES ('NULL', 'NULL', 'NULL', 'NULL', '$info')");
echo("Profile updated!");
?>[/code]
Link to comment
Share on other sites

'NULL' is not a value. That's a setting. Strip out the reference to your non-used table fields and simply have the INSERT specify just the info field.

[code]
<?
$info = $_POST["info"];
include("dbconnect.php");
$profile=MYSQL_QUERY("INSERT INTO account 'info' VALUES '$info ");
echo("Profile updated!");
?>[/code]
Link to comment
Share on other sites

I'm still having problems with it. profile.php has actually a little more code to it. Here's the rest:

[code]
<?
if      ($signup) { signup($finish, $user, $password1, $password2, $email);}
else if ($delete) { delete($finish, $user, $password);}
else if ($forgot)  { forgot($finish, $email);}
else                { login($finish, $user, $password);}
?>
<?
function content($user)
{
session_start();
session_register("user");

?>
<form action="addprofile.php" method="post">
$user = $_POST["user"];
<b>Character Profile:</b><BR><textarea name='info' rows='5' cols='50'></textarea>
<br/>
<br/>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<?
}
?>
<?
function login($finish, $user, $password)
{
include("dbconnect.php");

if ($finish) {

$query = mysql_query ("SELECT * FROM account WHERE user = '$user'");
$line = mysql_num_rows($query);
if ($line <= 0) {
echo "Unknown username.";
}
else {
while ($row = mysql_fetch_object ($query)) {
if ($row->password==$password) {
content($user);
}
else {
echo "Wrong password. Please try again.";
}
}
}

}

else {
echo "Sign up <a href=\"$PHP_SELF?signup=yes\">here</a>.";
echo "<p><h2>Login</h2></p>";

echo "<form method=\"POST\" action=\"$PHP_SELF?finish=yes\">";
?>
<table cellspacing="1" cellpadding="2" border="0">
    <tr>
    <td><font size="2">Username:</font></td>
    <td><font size="2"><input type="text" name="user" value="">&nbsp;</font></td>
  </tr>
  <tr>
    <td><font size="2">Password:</font></td>
    <td><font size="2"><input type="password" name="password" value=""></font></td>
  </tr>
  <tr>
    <td colspan=2 align=center><input type=submit value=Login></td>
  </tr>
</table>
</form>
<?
echo "<p><a href=\"$PHP_SELF?forgot=yes\">Forgot password?</a><br><a href=\"$PHP_SELF?delete=yes\">Delete Account</a></p>";
}

}
?>
<?
function signup ($finish, $user, $password1, $password2, $email)
{
include("config.php");

if($finish) {
$query1 = mysql_query("SELECT user FROM login");
while ($row = mysql_fetch_object ($query1)) {
if ($row->user==$user) {
echo "This user already exists.";
exit;
}
}
if ($user=="" OR $password1=="" OR $password2=="" or $email=="") {
echo "One field not completed. Please press back and fill out everything.";
}
else if ($password1!=$password2) {
echo "The passwords did not match.";
}
else {
$anfuegen=mysql_query("INSERT INTO account (user, password, email) VALUES ('$user','$password1', '$email')");
echo "Success! You may now log in.<br>";
@login();
}
}
else {
echo "<h2>Sign up</h2>";
echo "<form method=\"POST\" action=\"$PHP_SELF?signup=yes&finish=yes\">";
?>
<table cellspacing="1" cellpadding="2" border="0">
    <tr>
    <td><font size="2">Username:</font></td>
    <td><font size="2"><input type="text" name="user" value="">&nbsp;</font></td>
  </tr>
  <tr>
    <td><font size="2">Password:</font></td>
    <td><font size="2"><input type="password" name="password1" value=""></font></td>
  </tr>
  <tr>
    <td><font size="2">Confirm password:</font></td>
    <td><font size="2"><input type="password" name="password2" value=""></font></td>
  </tr>
  <tr>
    <td><font size="2">E-Mail:</font></td>
    <td><font size="2"><input type="text" name="email" value="">&nbsp;</font></td>
  </tr>
  <tr>
    <td colspan=2 align=center><input type=submit value=signup></td>
  </tr>
</table>
</form>
<?
}
}
?>
<?
function forgot ($finish, $email)
{

include("dbconnect.php");

if ($finish) {
$query=mysql_query("SELECT * FROM account");
while ($row = mysql_fetch_object ($query)) {
if ($email==$row->email) { $ismail="true"; }
}
if ($ismail=="true") {
$password=mysql_query("SELECT * FROM account WHERE email = '$email'");
while ($row = mysql_fetch_object ($password)) {
$message="Hello $row->user!\n\nYour password is:\n\n$row->password\n\nSincerely,\nAdmin";
}
mail($email, "Password", $message, "From: Administrator");
?>
<p>Your password has been sent to <i><? echo $email; ?></i> successfully.<br>
Please check your email and login again.<br></p>
<?

@login();
}
else { echo "The indicated email was not found. Please try again.";}
}
else {

echo "<p><h2>Forgot Password</h2><p>";
echo "<p><form method=post action=\"$PHP_SELF?forgot=yes&finish=yes\">";
?>
<table cellspacing="1" cellpadding="2" border="0">
<tr><td><font size="2">E-Mail Address:</font></td><td align=center><input type=text name=email></td></tr>
<tr><td colspan="2" align=center><input type=submit value="Submit"></td></tr>
</table></form></p>
<?
}
}
?>
<?
function delete($finish, $user, $password)
{

include ("config.php");

if($finish) {
$query = mysql_query ("SELECT * FROM account WHERE user = '$user'");
$line = mysql_num_rows($query);
if ($line <= 0) {
echo "Unknown username.";
}
else {
while ($row = mysql_fetch_object ($query)) {
if ($row->password==$password) {
$delete = mysql_query ("delete FROM account WHERE user = '$user'");
echo "Successfully removed from the database.";
}
else {
echo "Bad password!";
}
}
}

}
else {
echo "<p><h2>Delete Account</h2></p>";
echo "<form method=\"POST\" action=\"$PHP_SELF?delete=yes&finish=yes\">";
?>
<table cellspacing="1" cellpadding="2" border="0">
    <tr>
    <td><font size="2">Username:</font></td>
    <td><font size="2"><input type="text" name="user" value="">&nbsp;</font></td>
  </tr>
  <tr>
    <td><font size="2">Password:</font></td>
    <td><font size="2"><input type="password" name="password" value=""></font></td>
  </tr>
  <tr>
    <td colspan=2 align=center><input type=submit value=delete></td>
  </tr>
</table>
</form>
<?
}

}
?>
[/code]

I think that the problem is that it doesn't know where to actually insert in ID wise.. Anything up with this code?
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.