Jump to content

WesleyA

Members
  • Posts

    172
  • Joined

  • Last visited

Posts posted by WesleyA

  1. I believe it is possible, but the thing is that there seems to be a difference in getting rows from a databases and getting tables.

     

    Getting a row like id or name in a table has an identification variable.

     

    When you use SHOW TABLES you get all the tables but its a list, an array.

  2. Actually I had that already.

     

    This is a part of my own script

        <html>    <!-- make connection -->    <?php    $sql = "SHOW TABLES";    if (!$result = $conn->query($sql)){	die('There was an error running the query[' .$conn->error. ']');		    }    while($row = $result->fetch_assoc()){	echo "<center>";	$reeks = implode(" " , $row);	echo $reeks;		echo "<br>";	echo "</center>";    }    mysqli_close($conn);				?>		<body>		<table style="width: 100%;">    <tbody>    <tr>    <td>    <div class="lefcol"> <center>    <br><br><br><br><br><br>    Choose here which option you want<br>    <br><br>    <?php       ?>	         <form method="post">       <select name="col2" >	      <?php 	if (is_array($row)) //	{		foreach ($row as $value) : ?>					        <option value="<?php echo $value ?>"> <?php echo $value ?></option> 	        <?php endforeach; }?>        </select>    	<br><br><br>       			<input type="submit" value="click" />        </form>    <!-- continue html choice -->    <br>    </center>    </html>

    so I'm able to print or echo the database tables. Now the thing is that I want these tables in the select tag.

  3. i found this script online: http://stackoverflow.com/questions/8022353/how-to-populate-html-dropdown-list-with-values-from-database

        <?php     $conn = new mysqli('localhost', 'username', 'password', 'database')      or die ('Cannot connect to db');     $result = $conn->query("select id, name from table");     echo "<html>";     echo "<body>";     echo "<select name='id'>";     while ($row = $result->fetch_assoc()) {                  unset($id, $name);                  $id = $row['id'];                  $name = $row['name'];                   echo '<option value="'.$id.'">'.$name.'</option>';     }     echo "</select>";     echo "</body>";     echo "</html>";     ?> 

    But what I want is to get the table names from the database. Does anyone know how?

  4. Because one arry is done exactly as it supposed to be inserted and one not I have the idea that the for each loop should be extended.

     

    The options you have given result in the same problem, namly that 1 array is inserted okay, and one not.

     

    Maybe I can indeed combine it with the autoincrement option Do you have any idea how programmers would employ an autoincrement table variable in a foreach loop.

     

    I had a construction with 2 for each loops but this resluted in adding the other array on extra rows.

     

    So instead of the wrong example I uploaded; I would like to have the following output

     

     

    linkoms: ---------------- linkadd

    ++++++++++++++++++++++++++++++++++++++++++++++++

    wine --------------------www.wine.com

    white wine -------------www.whitewine.com

    red wine ---------------www.redwine.com

    etc.

     

     

     

     

     

  5. I found this script here: http://www.w3schools.com/php/php_mysql_prepared_statements.asp

     

    I shall quote it

    Example (MySQLi with Prepared Statements)     <?php     $servername = "localhost";     $username = "username";     $password = "password";     $dbname = "myDB";       // Create connection      $conn = new mysqli($servername, $username, $password, $dbname);       // Check connection       if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error);           }       // prepare and bind                $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)"        );         $stmt->bind_param("sss", $firstname, $lastname, $email);          // set parameters and execute      $firstname = "John";      $lastname = "Doe";      $email = "john@example.com";       $stmt->execute();      $firstname = "Mary";      $lastname = "Moe";      $email = "mary@example.com";      $stmt->execute();      $firstname = "Julie";      $lastname = "Dooley";      $email = "julie@example.com";      $stmt->execute();        echo "New records created successfully";       $stmt->close();        $conn->close();        ?>

    A set up like this might work for me maybe.

     

    But in this script isnt any definition to find for instance for $stmt. I tried if(isset) but I'm not sure if that is the solution. How is a variable like $stmt defined in thi matter?

  6. Yes. Foreach loops over a single array. If you need to access elements from multiple arrays then use for instead.Tables and columns don't have IDs. You can make a unique ID on rows if you set up a column to be either a unique or primary key.

     

    Yes I understand that a bit more. I managed to creat a primary key which is incremented automatically. SO the rows are auto incremented.

     

    But I want more.

     

    I think it's useful to give a table also a unique number and I would want to figure out a way I which this is possible. I hope it is namly, and I think it's illogical when mysq doesnt give the possibilities.

  7. Why are you placing all links in a array? where are these collected from? form input? you could create table with these predefined a id number, linkoms and corresponding single link, then all you have to do is use id ref.

     

    Okay I'll do that, but what exactly is meant with id ref?

     

    And what should the table look like what kind of counter is regularly used in these cases?

  8. I made a screen print, the phpmyadmin settings are in my own dutch language but it helps explaining what my puprpose is:

     

    Wine_Table_Screen_Print3.gif

     

    the right column contains the entire array of five links, while it shoudl contain only one. The name (linkoms) should correspondent with the linkaddress (linkadd).

  9. The condition section now looks like this:

                            $name_A = $_POST['name_A'];			$name_B = $_POST['name_B'];									$newvar2 = implode (", " , $name_A);			$newvar3 = implode (", " , $name_;									foreach ($name_A as $newvar2) {																		$sql = "INSERT INTO table_name (name_A, name_                                        VALUES ('$newvar2', '$newvar3') ";								if ($conn->query($sql) === TRUE) 								{  // to be continued

    resulting in putting an array in the name_B, but name_A is now correct which is inserting only just one string element each time.

     

     

    If I understand PHP a bit then inthis script foreach is the term that for each arrayname every element is .. then the statement

     

     

    But this only happens for $name_A as $newvar2, and that's it.

     

    This doesnt happen for $name_B and $newvar3.

     

    Doing it outside the loop with a second for eacht condition is adding to a new set of five rows. (if you make 2 separate sql queries for $newvar2 and $newvar3).

  10. Okay, but what happens then because I want to use the next query

          $sql = "INSERT INTO table_name (colname1, colname2) VALUES ('$vara', $varb ) ";

    What I try to say is that I have 2 arrays which have to end up in 2 columns but will that work if you make one array like $varc of it?

  11. I have a problem with passing 2 arrays in one mysql query.

     

    I have to use foreach and implode 2 times before having the query put in mysql.

     

    Is it possible to use a syntax like

    $varc = implode(", " , $vara, $varb)

    or

                foreach($x as $vara, $varb)

    I tried it but it didnt work, so I wonder what alternatives there are?

  12. My script gives a sort of an errormessage.

     

    Actually i can see a part of the code at the output screen in the browser.

     

    Like wth the php parser you get a notice or fatal error, but in this case the script does not work.

     

    How can i avoid these problems in the future?

×
×
  • Create New...