0

I'm trying to creat a form dynamically depending on the number of rows of a table in a database. I tried this and it's nor working:

require_once('mysqli_connect.php');

//I select the colum w_spanish from the table selected by the user
$q="SELECT w_spanish FROM ".$_GET['name'];

$r=@mysqli_query($dbc, $q);

echo '<FORM METHOD="POST" ACTION="Correction.php">';
echo '<TABLE BORDER="1">';

//Here is where I generate dinamically a table that can be filled by user
while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){

$aux=$row['w_spanish'];
echo '<TR><TD>'.$aux.'</TD><TD><INPUT TYPE="TEXT" NAME="Sol_'.$aux.'" SIZE="20"></TD></TR>';

}

echo '</TABLE>';
echo '<P><INPUT TYPE="SUBMIT" VALUE="Submit" ></P></FORM>'; 


mysqli_close($dbc);

So when I press submit, the information is not sent to "Correction.php", and I think it's because I creating the HTML form inside php code. How could I do it right??

11
  • What happens if you push submit? Commented Jul 18, 2013 at 14:40
  • have you verified if the query returns correct result? Commented Jul 18, 2013 at 14:41
  • @Bart Friederichs an error trying to call a file that doesn't exist. But Correction.php doest exist (and it's placed in the forrect folder in the server). If for example I creat the same form, but in all in HTML, and generating the table with a fixed number of row, everything works fine, and I reach Correction.php perfectly. Commented Jul 18, 2013 at 14:44
  • Which file is it trying to fetch then? Commented Jul 18, 2013 at 14:46
  • you can create html form inside php code. I don't think that is the problem here. Commented Jul 18, 2013 at 14:48

3 Answers 3

2

First off - remove the @ from the @mysqli statement as it is masking any errors that maybe happening.

Secondly take the generated code and paste it into http://validator.w3.org/#validate_by_input and see if there are any HTML errors and adjust where necessary.

Thirdly, since the user can select which table to read then your data needs to be super-sanitised as you certainly don't want sql injection attacks here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! for all the info. I'm new with PHP and my code is still very messy
1

The problem may be the query you are running. Without knowing more information, my guess would be your query isn't getting anything. Try dumping the row in each iteration and see what spits out. You may be looking for something like:

$q="SELECT w_spanish FROM tableName WHERE name = " . $_GET['name'];

If that's not it, it could also be the fact that since you are only grabbing one column from the database, you don't need access the information with $aux=$row['w_spanish'];. You can just use:

$aux=$row;

That I'm not 100% on though. Try dumping each row with var_dump() and see what pops out.

Comments

-2

First declare $row, then use a do-while loop.

$row = mysqli_fetch_array($r, MYSQLI_ASSOC) do{  
    $aux=$row['w_spanish']; 
    echo '<TR><TD>'.$aux.'</TD><TD><INPUT> TYPE="TEXT"NAME="Sol_'.$aux.'" SIZE="20"></TD></TR>';
 }while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC))

1 Comment

How does this change anything? manually fetching a row then using a do/while loop is functionally the same as just using a normal while() loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.