Populating a pull down with a table, inserting into another, +1

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 443 Views - Last Post: 03 February 2012 - 10:41 AM Rate Topic: -----

Topic Sponsor:

#16 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: Populating a pull down with a table, inserting into another, +1

Posted 01 February 2012 - 05:53 AM

I hate to ask this though, you didn't answer one of my questions:

$id = mysqli_real_escape_string($dbc, trim($_POST["userid"]));
$id = mysqli_real_escape_string($dbc, trim($_GET["userid"]));



Should I enter this into the script?


As for your other points, the database name is one thing, the table is user and testbed, so should what you posted above about the database name be changed?
Was This Post Helpful? 0
  • +
  • -

#17 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 380
  • View blog
  • Posts: 961
  • Joined: 08-March 09

Re: Populating a pull down with a table, inserting into another, +1

Posted 01 February 2012 - 06:18 AM

Hi,

Trying to access $_POST["userid"] would only make sense if you expect there to be a "userid" index in the $_POST array. Assuming you are trying to access data from a form being submitted the form would need to have a input with the name "userid". Otherwise you would have to look for an index in the $_POST array that corresponds to the form element you are trying to access.

I don't think you need to change the database name no. Because your query successfully fetches data from the user table we can assume that you are selecting the correct database.
Was This Post Helpful? 0
  • +
  • -

#18 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: Populating a pull down with a table, inserting into another, +1

Posted 01 February 2012 - 06:43 AM

View PostJstall, on 01 February 2012 - 06:18 AM, said:

Hi,

Trying to access $_POST["userid"] would only make sense if you expect there to be a "userid" index in the $_POST array. Assuming you are trying to access data from a form being submitted the form would need to have a input with the name "userid". Otherwise you would have to look for an index in the $_POST array that corresponds to the form element you are trying to access.

I don't think you need to change the database name no. Because your query successfully fetches data from the user table we can assume that you are selecting the correct database.


There is a difference between a database and a table though right?

Also, thanks for the help, but I know you are trying to tell me how to fix the script without doing so, but I'm still as lost as I was when we first started talking.

I've been staring at the script attempting to look at it to solve the issue, and also solve the reason why it won't post, but it is eluding me.

You don't need to tell me how to fix it, just point to the lines in where I need to fix to solve the issues in question, the rest I should be able to figure out myself, I am just running into a situation in where staring at the same script over and over again is making me doubt my own skills because I can't see what you do.
Was This Post Helpful? 0
  • +
  • -

#19 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 380
  • View blog
  • Posts: 961
  • Joined: 08-March 09

Re: Populating a pull down with a table, inserting into another, +1

Posted 01 February 2012 - 07:14 AM

Hi,

Yes there is a difference between a database and a table. Databases use tables to store data, a database can have many tables. Once you successfully connect to a database you can run queries against it, including those that access data in the tables. I don't believe this is an issue for you though, from what I can see you are connecting to the proper database, that part is working.

From the code you showed earlier you have a <form> with a <select> element on it with the name fab1. That means that when the form is submitted to the server the selected <option>'s value is accessible in $_POST['fab1']. For your purposes you could just create a variable to hold the id and use that to compare when you are going through your result set.

This is a slight modification of your code to make the required checks:
<?php
require_once('tb/connectvars.php');
	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
	$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
		$mysqli->select_db('pawz_PDI');

		$id= mysqli_real_escape_string($dbc, trim(0));
			if(isset($_GET['id']))
		$id= mysqli_real_escape_string($dbc, trim($_GET["id"]));
		if (isset($_POST['submit'])) {
			$id= mysqli_real_escape_string($dbc, trim($_POST["id"]));
		}
		if (isset($_POST['fab1'])) {
			//this is the id from the select box
			$selected_id = $_POST['fab1'];
		}
if (isset($_POST['submit'])) {
	$fab1= 'fab1';
$query = "UPDATE testbed SET fab1 = '" . mysqli_real_escape_string($dbc, $fab1) . "' WHERE id = '$id'";

// echo your raw query and look for obvious errors
     echo "Query is : " . $query . "<br />";

}
echo '<h2>Fabricators</h2>';
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
	echo '<div id="box4">';
		echo '<div id="fab1">';
			$result = $mysqli->query("SELECT * FROM user"); 
		echo "<select name='fab1'>\n";
			while($row = $result->fetch_assoc())
{
		echo '<option value="' . $row['fab1'] . '"';
			//compare the id in the current record to the one from the drop down 
			if($row['userid'] == $selected_id){
				echo ' selected';
				}
		echo '>' . $row['user'] . '</option>\n';
}
echo '<div id="button3"><input type="submit" value="Submit Test" name="submit" /></div>';
		echo '</div>';
	echo '</div>';
echo '</form>';

?>


Was This Post Helpful? 1
  • +
  • -

#20 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: Populating a pull down with a table, inserting into another, +1

Posted 02 February 2012 - 10:37 AM

Ok, prior to your edits my code works, well it works to the most extent, what is happening now is that it shows the drop down menu with a name, but not the name that is referenced in the testbed table. The name is referenced by a number, which is the userid field in table user. Which once referenced gives the script or is meant to.

The problem comes that when viewed it shows the wrong name. Instead it is pulling info straight from the user table, instead of testbed.

The example of what is going on is simple:

  • When I type in the web address, and end with ?id=1 it shows --None--
  • When I type in the web address, and end with ?id=2 it shows Andy Khal
  • etc.
  • etc.
  • etc.


Now ID in the userid field in the table user is:

  • 1 = --None--
  • 2 = Andy Khal

So to give you a reason what it should be is this:

When I type in the web address, and end with ?id=1 it should show Andy Khal not --None--

So knowing that I need to figure out why this is happening.

    <?php
    require_once('tb/connectvars.php');
    	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    	$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
    		$mysqli->select_db('testbed');
    
    		$id= mysqli_real_escape_string($dbc, trim(0));
    			if(isset($_GET['id']))
    		$id= mysqli_real_escape_string($dbc, trim($_GET["id"]));
    		if (isset($_POST['submit'])) {
    			$id= mysqli_real_escape_string($dbc, trim($_POST["id"]));
    		}
    		if (isset($_POST['fab1'])) {
    			//this is the id from the select box
    			$selected_id = $_POST['fab1'];
    		}
    if (isset($_POST['submit'])) {
    	$fab1= 'fab1';
    $query = "UPDATE testbed SET fab1 = '" . mysqli_real_escape_string($dbc, $fab1) . "' WHERE id = '$id'";
    
    // echo your raw query and look for obvious errors
         echo "Query is : " . $query . "<br />";
    
    }
    echo '<h2>Fabricators</h2>';
    echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
    	echo '<div id="box4">';
    		echo '<div id="fab1">';
    			$result = $mysqli->query("SELECT * FROM user"); 
    		echo "<select name='fab1'>\n";
    			while($row = $result->fetch_assoc())
    {
    		echo '<option value="' . $row['fab1'] . '"';
    			//compare the id in the current record to the one from the drop down 
    			if($row['userid'] == $selected_id){
    				echo ' selected';
    				}
    		echo '>' . $row['user'] . '</option>\n';
    }
    echo '<div id="button3"><input type="submit" value="Submit Test" name="submit" /></div>';
    		echo '</div>';
    	echo '</div>';
    echo '</form>';
    
    ?>


This post has been edited by Jstall: 02 February 2012 - 02:40 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#21 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 380
  • View blog
  • Posts: 961
  • Joined: 08-March 09

Re: Populating a pull down with a table, inserting into another, +1

Posted 02 February 2012 - 03:08 PM

Hi,

The file we are talking about has always pulled data from the user table. The query has always been "SELECT * FROM user".

If you want the functionality to change depending on what is in the query string is you need to access $_GET. $_GET is similar to $_POST in that it is a globally accessible associative array. However $_GET pulls values from the query string in the url. A query string is typically in the form of key-value pairs. For example : id=1 - id is the key and the value is 1.

$_GET has indexes that map to keys in the query string. So:
$id = $_GET['id']


Would take the value of id, in the query string, and assign it to the variable $id.
Was This Post Helpful? 1
  • +
  • -

#22 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: Populating a pull down with a table, inserting into another, +1

Posted 03 February 2012 - 07:52 AM

View PostJstall, on 02 February 2012 - 03:08 PM, said:

Hi,

The file we are talking about has always pulled data from the user table. The query has always been "SELECT * FROM user".

If you want the functionality to change depending on what is in the query string is you need to access $_GET. $_GET is similar to $_POST in that it is a globally accessible associative array. However $_GET pulls values from the query string in the url. A query string is typically in the form of key-value pairs. For example : id=1 - id is the key and the value is 1.

$_GET has indexes that map to keys in the query string. So:
$id = $_GET['id']


Would take the value of id, in the query string, and assign it to the variable $id.


But how would it work with getting the ID from testbed and getting userID from user. This is where things get confusing for me. To me that means if this is even possible.
$id = $_GET['id'] FROM testbed
$id1 = $_GET['userid'] FROM user


Was This Post Helpful? 0
  • +
  • -

#23 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 380
  • View blog
  • Posts: 961
  • Joined: 08-March 09

Re: Populating a pull down with a table, inserting into another, +1

Posted 03 February 2012 - 09:50 AM

Hi,

Well if you wanted them both specified in the query string you could make it something like "?testbed_id=1&user_id=2". But I don't think that is what you are trying to do. Sorry, could you clarify what it is you are trying to do? Are you intending on passing in the id for the testbed table in the query string and then get any users associated with it?
Was This Post Helpful? 0
  • +
  • -

#24 MattRidge  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 27-January 12

Re: Populating a pull down with a table, inserting into another, +1

Posted 03 February 2012 - 10:41 AM

View PostJstall, on 03 February 2012 - 09:50 AM, said:

Hi,

Well if you wanted them both specified in the query string you could make it something like "?testbed_id=1&user_id=2". But I don't think that is what you are trying to do. Sorry, could you clarify what it is you are trying to do? Are you intending on passing in the id for the testbed table in the query string and then get any users associated with it?



What I am trying to do is this:

Testbed is where all the data inputted resides. In the testbed table there is a field in there called fab1, which holds a number, 1 - 30. Each of those numbers correlate to a name in another table called user. Inside user, there are two fields we need to worry about here. userid, and user. userid are the numbers, user is the full name of the number.

So when a script query the testbed table and we get to the fab1 field, it should pull number 2 out of fab1 and then say I need the name that correlates number 2 from table user.

Once that happens user comes back and says number 2 is Andy Karl.

From there the script will show in a pull down menu with the name Andy Karl as it's selection, instead of what is at spot 1 in the user table which has the name of --None--.

From there a person should be able to click on the pull down menu and then be able to select any name in the list, and press submit. Once submit is pressed it will update the fab1 field in the testbed table with the new number.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2