Jump to content

Problems with INSERT INTO


bhenry

Recommended Posts

 $sql = "INSERT INTO 284.284_users (id, username, password, first, last, priv) VALUES (NULL, '$updateusername', '$updatepassword', '$updatefirstname', '$updatelastname', '$priv')";
mysql_select_db($dbname);
mysql_query($sql);

 

this does not work. i've made sure that the db columns are correct. everything is spelled exact. my connection works, as i am able to pull and update records which are already in there. i've copied this code from a working application which i wrote last month. can anyone see something i'm missing? i can post the entire code here (the page is not complete, but i'd like to get the edit user page to be able to add new users before i waste my time writing everything else):

 

<?php
//this site is to keep all 284-Lifers updated on FAQs and resources approved by Mikel.
//start session at beginning to avoid clearing out any set vars
session_start();

//connect
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "284";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR />");
mysql_select_db($dbname);

//if logout button is pressed then logout!
if ($_POST['logout'])
{//log them out
$_SESSION['password'] = '';
$_SESSION['username'] = '';
$_SESSION['first'] = '';
$_SESSION['last'] = '';
$_SESSION['userid'] = '';
$_SESSION['loggedin'] = false;
}//end logout

//if login button is pressed authenticate
if ($_POST['login'])
{
//set temp vars for login authentication
$user = $_POST['username'];
$pass = $_POST['password'];

//search for user in db
$auth = "SELECT * FROM 284_users WHERE username ='$user'"; 
$check = mysql_query($auth);

//set array with user info from db
$row = mysql_fetch_array($check);

if ($row['password'] == $pass)
{//password correct. log them in!
$_SESSION['password'] = $pass;
$_SESSION['username'] = $user;
$_SESSION['first'] = $row['first'];
$_SESSION['last'] = $row['last'];
$_SESSION['userid'] = $row['id'];
$_SESSION['userpriv'] = $row['priv'];
$_SESSION['loggedin'] = true;
}

}//end login buttonpress


//set user variables if there is a user
if ($_SESSION['username'] != '')
{//someone is logged in....

//set userinfo for simple use
$username = $_SESSION['username'];
$first = $_SESSION['first'];
$last = $_SESSION['last'];
$userid = $_SESSION['userid'];
$userpriv = $_SESSION['userpriv'];
$loggedin = $_SESSION['loggedin'];

}//end logged in user settings.


?>

<html>
<head>
<LINK REL="StyleSheet" HREF="284-style.css" TYPE="text/css" />
</head>
<body>
<?php if($loggedin) {echo '<div class="greet">Hello ' , $first , "!</div>";} ?>
<table cellpadding="0px" width="600px">
<form name='page' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>

<tr class="login">
<td colspan="3">
Username: <input type="text" name="username" value="<?php echo $username; ?>" /> 
Password: <input type="password" name="password" value="" />
<?php if (!$loggedin){ ?>
<input type="submit" name="login" value="Login" />
<?php }//end login button for no logged in user ?>
<?php if ($loggedin){ ?>
<input type="submit" name="logout" value="Logout" />
<?php }//end user logged in ?>



<?php if ($userpriv == 'admin'){ ?>
<input type="submit" name="admin" value="Administration" />
<?php }//end if admin logged in ?>



</td>
</tr>

<tr class="head">
<td><input type='submit' name='questions' value='284-WHAT?' /></td>
<td><input type='submit' name='suggestions' value='284-ISPY' /></td>
<td><input type='submit' name='updates' value='284-NEWS' /></td>
</tr>

<tr class="content">
<td colspan="3">

<?php if($_POST['admin'] || $_POST['edituser'] || $_POST['deleteuser'] || $_POST['updateuserbutton']) {//show admin page ?>

<table width="100%">
<tr>
<td class="chooseuser">
Choose User:<br /><select name="useredit" id="useredit">
<?php
$users = "SELECT * FROM 284_users ORDER BY last"; 
$choose = mysql_query($users);

echo "<option value='newuser'>Add New User</option>\r\n";

//get current users
while($person = mysql_fetch_array($choose))
  	{
$edituserID = $person['id'];
$editFirst = $person['first'];
$editLast = $person['last'];	
  	echo "<option value='$edituserID'>$editLast, $editFirst</option>\r\n";
  	}
?>
</select><br /><input type="submit" name="edituser" value="Edit" /> <input type="submit" name="deleteuser" value="Delete" />
</td>

<?php if($_POST['edituser'])
{//get values and show form for editing
$edituserid = $_POST['useredit'];

if($edituserid == 'newuser')
{//let's clear fields for new user
$editusername = '';
$editfirstname = '';
$editlastname = '';
$editpassword = '';
}
else
{//name selected so let's find it
$getuser = "SELECT * FROM 284_users WHERE id = '$edituserid'";
$finduser = mysql_query($getuser);
$u = mysql_fetch_array($finduser);
$editusername = $u['username'];
$editfirstname = $u['first'];
$editlastname = $u['last'];
$editpassword = $u['password'];
if($u['priv'] == 'admin'){$editpriv = 'CHECKED';}else{$editpriv = '';}
}
?>

<td class="edituser">
Username: <input type="text" name="editusername" value="<?php echo $editusername; ?>" /><br />
First name: <input type="text" name="editfirstname" value="<?php echo $editfirstname; ?>" /><br />
Last name: <input type="text" name="editlastname" value="<?php echo $editlastname; ?>" /><br />
Password: <input type="text" name="editpassword" value="<?php echo $editpassword; ?>" /><br />
<input type="hidden" name="edituserid" value="<?php echo $edituserid; ?>" />
Admin? <input style="border:none;" type="checkbox" name="editpriv" value="admin" <?php echo $editpriv; ?> />   <input type="submit" name="updateuserbutton" value="Update User" /><br />
</td>
<?php }//end edit user section ?>


<?php if($_POST['updateuserbutton'])
{//get values and show form for updating
$updateuserid = $_POST['edituserid'];
$updateusername = $_POST['editusername'];
$updatefirstname = $_POST['editfirstname'];
$updatelastname = $_POST['editlastname'];
$updatepassword = $_POST['editpassword'];
$priv = $_POST['editpriv'];
if($_POST['editpriv']=='admin'){$updatepriv = 'CHECKED';}else{$updatepriv = '';}

//check if name exists
$dupename = false;
$namecheck = "SELECT * FROM 284_users WHERE last = '$updatelastname' AND id != '$updateuserid'";
$checkname = mysql_query($namecheck);
while ($listnames = mysql_fetch_array($checkname))
{
if ($listnames['first'] == $updatefirstname)
{
$erroradding = "<div class='message'>This name already exists. Please try again with a middle initial after the firstname.</div><br />\r\n";
$dupename = true;
}
}

if($edituserid == 'newuser')
{//check for dupes and move on
if(!$dupename)
{//let's add the new emp to db
$sql = "INSERT INTO 284.284_users (id, username, password, first, last, priv) VALUES (NULL, '$updateusername', '$updatepassword', '$updatefirstname', '$updatelastname', '$priv')";
mysql_select_db($dbname);
mysql_query($sql);
}//end adding new emp if no dupes
}//end if addnew was chosen
else
{//name selected so let's find it to update it instead of adding a new one
//check for dupe names in case of updated name change
if(!$dupename)
{//no dupe so let's update it!
mysql_query("UPDATE 284_users SET username = '" . $updateusername . "' WHERE id = '" . $updateuserid . "'");
mysql_query("UPDATE 284_users SET first = '" . $updatefirstname . "' WHERE id = '" . $updateuserid . "'");
mysql_query("UPDATE 284_users SET last = '" . $updatelastname . "' WHERE id = '" . $updateuserid . "'");
mysql_query("UPDATE 284_users SET password = '" . $updatepassword . "' WHERE id = '" . $updateuserid . "'");
mysql_query("UPDATE 284_users SET priv = '" . $priv . "' WHERE id = '" . $updateuserid . "'");
}//end update of old user when no dupe

}
?>

<td class="edituser">
Username: <input type="text" name="editusername" value="<?php echo $updateusername; ?>" /><br />
First name: <input type="text" name="editfirstname" value="<?php echo $updatefirstname; ?>" /><br />
Last name: <input type="text" name="editlastname" value="<?php echo $updatelastname; ?>" /><br />
Password: <input type="text" name="editpassword" value="<?php echo $updatepassword; ?>" /><br />
<input type="hidden" name="edituserid" value="<?php echo $updateuserid; ?>" />
Admin? <input style="border:none;" type="checkbox" name="editpriv" value="admin" <?php echo $updatepriv; ?> />   <input type="submit" name="updateuserbutton" value="Update User" /><br />
</td>
<?php }//end update user section ?>


<?php if($_POST['deleteuser'] && $_POST['useredit'] != 'newuser')
{//get values and show form for editing
$edituserid = $_POST['useredit'];

//name selected so let's find it
$getuser = "SELECT * FROM 284_users WHERE id = '$edituserid'";
$finduser = mysql_query($getuser);
$u = mysql_fetch_array($finduser);
$editusername = $u['username'];
$editfirstname = $u['first'];
$editlastname = $u['last'];
$editpassword = $u['password'];

?>

<td class="edituser">
Username: <?php echo $editusername; ?><br />
First name: <?php echo $editfirstname; ?><br />
Last name: <?php echo $editlastname; ?><br />
<input type="hidden" name="edituserid" value="<?php echo $edituserid; ?>" />
<input style="background-color:#f00;" type="submit" name="deleteuserfinal" value="!!!Delete User!!!" />
</td>
<?php }//end delete user section ?>





<?php }//end if adminpage ?>

</tr>
</table>





</td>
</tr>
</form>
</table>


</body>
</html>

Link to comment
Share on other sites

not that i'm sure on this but is this a valid table name?

284.284_users

it has a dot in it, or is that a db dot notation type thing i've not come accross?

 

also see if this gives any more insight:

error_reporting(E_ALL);

 

can't see it, but have you got insert privileges, can you insert on other tables?

Link to comment
Share on other sites

it is db.table and as far as privileges go, i didn't set up my db any differently than the other dbs i've setup. i copied and pasted the insert statement that works in another application i wrote which runs on the same server and the db was setup through the same phpmyadmin. i'm pulling my hair out on this one, and it's driving me crazy!

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.