Custer Posted July 17, 2007 Share Posted July 17, 2007 Okay, I'm creating a registration script for users in my new php based game. What is making it difficult is that I created a MySQL table with possible initial stats and I'm trying to make a random number to select one of those initial id's and drop those stats into the users' data. So this is my script so far: <?php mysql_connect ("localhost", "*********", "******")or die("Could not connect: ".mysql_error()); mysql_select_db("custopoly_custopoly") or die(mysql_error()); include 'register.html'; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); if($username_exist > 0){ echo "I'm sorry but the username you specified has already been taken. Please pick another one."; unset($username); include 'register.html'; exit(); } // lf no errors present with the username // use a query to insert the data into the database. INSERT INTO users (id, email, username, password) VALUES('id', '$email', '$username', '$password'); $id = 'id'; $num=rnd BETWEEN 1 AND 20; $result = mysql_query("SELECT * FROM intialstats WHERE id='$num'"); mysql_fetch_array($result); $result2 = mysql_query("SELECT * FROM intialstats WHERE id2='$result'"); mysql_fetch_array($result2); INSERT INTO user_buildings (id, building_id) VALUES('$id', 'building_id'); mysql_close(); echo "You have successfully Registered"; header( 'Location: /' ) ; ?> And I'm just getting a blank screen with no data insertion.... Link to comment https://forums.phpfreaks.com/topic/60334-registerinsert-into-problem/ Share on other sites More sharing options...
gerkintrigg Posted July 17, 2007 Share Posted July 17, 2007 your 'id' in the insert statement is a string... I think you should start it with $ or define what $id is BEFORE the query. Does that help? Link to comment https://forums.phpfreaks.com/topic/60334-registerinsert-into-problem/#findComment-300162 Share on other sites More sharing options...
tapos Posted July 17, 2007 Share Posted July 17, 2007 Try This <?php mysql_connect ("localhost", "*********", "******")or die("Could not connect: ".mysql_error()); mysql_select_db("custopoly_custopoly") or die(mysql_error()); include 'register.html'; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); if($username_exist > 0){ echo "I'm sorry but the username you specified has already been taken. Please pick another one."; unset($username); include 'register.html'; exit(); } // lf no errors present with the username // use a query to insert the data into the database. mysql_query("INSERT INTO users (id, email, username, password) VALUES('$id', '$email', '$username', '$password')"); $id = 'id'; $num=rnd BETWEEN 1 AND 20; $result = mysql_query("SELECT * FROM intialstats WHERE id='$num'"); mysql_fetch_array($result); $result2 = mysql_query("SELECT * FROM intialstats WHERE id2='$result'"); mysql_fetch_array($result2); mysql_query("INSERT INTO user_buildings (id, building_id) VALUES('$id', '$building_id')"); mysql_close(); echo "You have successfully Registered"; header( 'Location: /' ) ; ?> -- Tapos Pal Link to comment https://forums.phpfreaks.com/topic/60334-registerinsert-into-problem/#findComment-300238 Share on other sites More sharing options...
Custer Posted July 17, 2007 Author Share Posted July 17, 2007 No luck. I'll try and give a little more information as to what I'm trying to do... First, I'm inserting the users pass, username and email into a table called users. include 'register.html'; $email = $_POST['email']; $username = $_POST['username']; $password = md5($_POST['password']); // lets check to see if the username already exists $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_exist = mysql_num_rows($checkuser); if($username_exist > 0){ echo "I'm sorry but the username you specified has already been taken. Please pick another one."; unset($username); include 'register.html'; exit(); ^^That piece of code, I took off of a registration tutorial script and modified it to fit my site, which really wasn't any modification. Then, in another table called initialstats, I have 20 different possible beginning stats every user can start off with. Which I'm using this piece of code to randomly select one of those id's: $id = 'id'; $num=rnd BETWEEN 1 AND 20; $result = mysql_query("SELECT * FROM intialstats WHERE id='$num'"); mysql_fetch_array($result); I was told I needed to fetch the result. Now because of the nature of the table initialstats, I had the following fields: id, id2, restype, amount, and building_id. Now for an example, I might start somebody off with 300 Coin, 200 Lumber, and 100 Stone...but if I kept doing that, my 'id' is auto increment, so it'd just keep raising the id number, so I made id2 and gave it the value of the 'id' field that I wanted it to go with. So ID '1' might have 300 stone and ID2 '1' would have 200 stone (same applies with the building_id, it matches with a table I have called buildings), and I'm trying to call on that match to and dump that into where I want it: $result2 = mysql_query("SELECT * FROM intialstats WHERE id2='$result'"); mysql_fetch_array($result2); Now, the even trickier part, I'm trying to dump this data I got from initialstats into a table called user_buildings. I want to take the ID that my user got in the users table and assign that id to all of hte information being dumped into user_buildings. So my user_buildings has the fields of: user_id, building_id, and amount. To rephrase myself, I'm dumping the user_id assigned in the users table to that and then dumping hte building_id that went with the id's in initialstats into this user_buildings, so later on, I can call that User51 has 3 quarries, per say... Anyone care to help out? Link to comment https://forums.phpfreaks.com/topic/60334-registerinsert-into-problem/#findComment-300400 Share on other sites More sharing options...
Custer Posted July 18, 2007 Author Share Posted July 18, 2007 Okay, sorry for double-posting. I fixed most of the problem: //random id $num=rand(1, 20); $result = mysql_query("SELECT * FROM initialstats WHERE id='$num'"); $initial_stats = mysql_fetch_assoc($result) var_dump($initial_stats); //defines variables $building_id = $initial_stats['building_id']; $restype = $initial_stats['restype']; $resamount = $initial_stats['resamount']; //fetches user_id $query = mysql_query("SELECT id FROM users WHERE username = '".$username."'"); $users = mysql_fetch_assoc($query); $id = $users['id']; //insertion to user_buildings mysql_query("INSERT INTO user_buildings (user_id, building_id, amount) VALUES('$id', '$building_id', '1')"); //insertion to user_resources id mysql_query("INSERT INTO user_resources (user_id, restype, resamount) VALUES('$id', '$restype', 'resamount')"); Now this is my problem. In the user_resources, I am trying to insert this data, but there are multiple rows in initialstats that have the same id # and I want all of them to be selected and inserted. I know I'm going to have to use a loop using while, but I don't know how to do this here properly. I need to loop the selection to select all of the id's with the same number and then insert all of their data... Link to comment https://forums.phpfreaks.com/topic/60334-registerinsert-into-problem/#findComment-300778 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.