Jump to content

Cannot write in database


Absorbator

Recommended Posts

Hello to everybody I have very stupid problem.

First at all I'd like to apology  about my bad english. There is the problem:

I can read MySql databases with php script, but I cannot write in them. I'm asking you is there a possibility the server has wrong settings (because I checked them and there was this "Read-Only-OFF") or my code is wrong (I have compare it with another codes: nothing illegal...).

 

 

Link to comment
https://forums.phpfreaks.com/topic/102513-cannot-write-in-database/
Share on other sites

This is the code, modified by me to be easier to read it

<?php
$reg_form="<table border=0 bgcolor=0099ff cellpadding=0 cellspacing=2 width=300 height=140>
<tr><td bgcolor=fff0f0 align=center><form action='example.php' method=post>Username:<input type=text size=16 maxlength=32 name=profile align=right>
<br>Password:<input type=password size=16 maxlength=32 name=pass>
<br>Confirm Password:<input type=password size=16 maxlength=32 name=configpass>
<br>E-mail:<input type=text size=16 maxlength=32 name=email>
<br><input type=submit value='registrate' name=submit></form></td></tr>
</table>";
$message = "<font color=blue>Enter your name, password and e-mail</font>";
if($_POST['submit'])
{
if(!$_POST['user'] | !$_POST['pass'] | !$_POST['configpass'] | !$_POST['email'])
  {
   $message = "<font color=red>You did not complete all of the fields!</font>";
  }
  else
{
$chars_number_pass = strlen($_POST['pass']);
$chars_number_user = strlen($_POST['user']);
if($_POST['pass'] != $_POST['configpass'])
{
$message = "<font color=red>You must enter you password twice(once in password field and once in Confirm Password field)</font>";
}
else
{
if($chars_number_pass < 6 || $chars_number_user < 3)
{
$message = "<font color=red>Your password or username (or both) is too short!</font>";
}
else
{
if (!$_POST['email'])
{
echo "<font color=red>You did not type an e-mail address!</font>";
}
else
{
$mailcheck = "@";
if(!ereg($mailcheck, $_POST['email']))
{
$message = "<font color=red>An invalid e-mail address</font>";
}
else
{
if(!get_magic_quotes_gpc())
{
$user=addslashes($_POST['user']);
$pass=addslashes($_POST['pass']);
$mail=addslashes($_POST['email']);
}
else {
$user=$_POST['user'];
$pass=$_POST['pass'];
$mail=$_POST['email'];
}
$check_user = mysql_query("SELECT username FROM accounts WHERE username IN($user)");
if($check_user == $_POST['user'])
{
$message = "<font color=red>The username $_POST['user'] is allready in use!</font>";
}
else
{
$reg=mysql_query("INSERT INTO accounts ('username', 'pass') VALUES ('$user', '$pass')");
if (!$reg)
{
$message="<font color=red>Failed to record into databases!</font>";
}
else
{
$message = "<font color=#487644>Your registration is now valid</font>";
$reg_form="";
setcookie (user, $user, time()+3600, "/", "", 1 );
setcookie (pass, $pass, time()+3600, "/", "", 1 );
}}
}
}
}}}}
echo "<p align=center>";
echo $message;
echo $reg_form;
echo "</p>";
}
?>

I think that the problem is in the code, because i have used PHPBB forum code which worked without problems

Whenever you're having problems with a query, you should add an or die statement and echo the query. That is, this:

 

$check_user = mysql_query("SELECT username FROM accounts WHERE username IN($user)");

 

Becomes:

$sql = "SELECT username FROM accounts WHERE username IN($user)";
$check_user = mysql_query($sql) or die(mysql_error());
echo '<br />Query was:'.$sql;

 

Now, when you do that, i suspect you'll find that $user is undefined, since your form has this:

 

Username:<input type=text size=16 maxlength=32 name=profile align=right>

 

Im guessing that should be:

 

Username:<input type=text size=16 maxlength=32 name=user align=right>

 

 

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.