Jump to content

WesleyA

Members
  • Posts

    172
  • Joined

  • Last visited

Everything posted by WesleyA

  1. OK but in this case the table is a variable.
  2. which query should be used when you have this INSERT INTO `$variable` (col1, col2) VALUES ..... The question is regarding $variable. is it " ", ' ' ,` ` . ?
  3. 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.
  4. 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.
  5. can the SHOW TABLES output be used to fetch with fetch_assoc? Or is more necessary?
  6. 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?
  7. I already fixed the issue I used mysqli_real_escape_string
  8. 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.
  9. 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?
  10. 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.
  11. Is it possible to make unique id's or keys (are they the same?) for 1) tables, 2) columns, 3) rows?
  12. Isnt it more useful to create a column extra. linkoms and linkadd are also columns, so id_num could also be a column do you agree with that?
  13. 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?
  14. I made a screen print, the phpmyadmin settings are in my own dutch language but it helps explaining what my puprpose is: 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).
  15. 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).
  16. I tried it but it didnt really work. it has to do with wanting these arrays on the same row. What you say is true, namly that you get an entire array but it is added completely to 1 row.
  17. 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?
  18. 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?
  19. actually it worked, I added the values to the wrong table.
  20. OK but I actually mean a column heading. What I look for is a query to create a column heading and a row heading. Is there any?
  21. Is it possible to create a table without columns?
  22. $sql = "INSERT INTO camping (linkname, addlink) VALUES ('camping Fantasia' , 'http://www.campingfantasia.com')"; I dont see any column like linkname or addlink in phpmyadmin. How can I check whether this exists or not?
  23. Unknown column 'col' in 'field list' Is the error message, i tried all kinds of backticks, and quotes but doesnt work So I was thinking, how can I check (in phpmyadmin or in the mysql console) whether a table has the columns that are needed?
  24. 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...