A newbie needs help with dynamic forms


#1

Hi im sorta new to programming, and i dont know any object oriented php yet.

I was having some trouble with this dynamic form script i am trying to make. The dynamic form works fine, but i want to have an edit button right next to each dynamic table entry, which is the troublemaker here.

[php]


<?php $row[1] ?>
<?php include("db.php"); $title=$_POST["title"]; $text=$_POST["text"]; $sqlInsert="INSERT INTO kontrakt VALUES ('$title', '$text');"; $sqlSave=mysql_query($sqlInsert); $sqlSentence="SELECT * FROM kontrakt;"; $sqlResult=mysql_query($sqlSentence) or die ("ikke mulig å lagre"); $numRows=mysql_num_rows($sqlResult); if ($numRows==0) { print("ingen kontrakt er laget"); } else { ?>
<table border="1px">
<form method="post" action="" name="points" id="points">
<?php
for ($r=1;$r<=$numRows;$r++)

{
$row=mysql_fetch_array($sqlResult);
print("

$row[0] $row[1] <input type=“submit” name=“rediger.$r” id=“rediger.$r” value=“Rediger” /> ");
}
}

?>

[/php]

Some of the names and id’s are in norwegian, but i’ve tried to keep all variable names in english.

All help is much appriciated :slight_smile:


#2

Just hit me that it wont know which $row to get at the top, because it generates alot of em with same names.

Still not getting anywhere tho


#3

[php]

<?php $row[1] ?>


<?php[/php] you should assign a variable ex: [php]$row1 = $row[1] ;[/php] [php]<?php echo ".$row[1]." ?>[/php]

how do you declare $row[1]?


#4

Thanks alot! :smiley:

Now for some reason it stopped connecting to the DB o,O


#5

Nevermind, tried making a variable and put it as value of the input / textarea and it didnt work…

Any other tips?


#6

show us the code which define array variable $row[1];

and try to see if you get the value of row[1];

[php]print_r ($row[1]);[/php]


#7

Ok i figured out how to get the value to the top (but i chose to put it into a different file for ease)…

Now i’ve run into a different problem.

Take a look at this:

[php]





<?php include("db.php"); $title=$_POST["title"]; $text=$_POST["text"]; $sqlSentence="SELECT * FROM kontrakt;"; $sqlResult=mysql_query($sqlSentence) or die ("klarer ikke å hente ut data fra databasen"); $sqlInsert="INSERT INTO kontrakt VALUES ('$title', '$text');"; $sqlSave=mysql_query($sqlInsert) or die ("klarer ikke å lagre i databasen"); $numRows=mysql_num_rows($sqlResult); if ($numRows==0) { print("ingen kontrakt er laget"); } else { ?>
<table border="1px">
<form method="post" action="edit.php" name="edit" id="edit">
<?php

for ($r=1;$r<=$numRows;$r++)

{
$row=mysql_fetch_array($sqlResult);
print("

<input type=“text” name=“row”."$r" id=“row”."$r" value="$row[0]" readonly=“true”/> <input type=“text” name=“row2”."$r" id=“row2”."$r" value="$row[1]" readonly=“true”/> <input type=“submit” name=“rediger$r” id=“rediger$r” value=“Rediger” /> $row1=$row[0]; $row2=$row[1]; <input type=“hidden” name=“r” id=“r” value="$r"/> ;");
}
}

?>

[/php]

and this:

[php]

<?php include("db.php"); $r=$_POST["r"]; $title=$_POST["row"."$r"]; $text=$_POST["row2"."$r"]; ?>

<input type=“text” name=“title” id=“title” value="<?php print("$title"); ?>" />

<?php print("$text"); ?>
<?php $main=$_POST["title"]; $sub=$_POST["text"]; $sqlEdit="UPDATE kontrakt SET tittel='$main', tekst='$sub' WHERE tittel='$title' AND tekst='$text';"; $sqlResult=mysql_query($sqlEdit) or die ("klarte ikke å endre på oppføring"); ?>

[/php]

The 2nd one is my edit file.

The issue now is that i cant choose witch dataset (inputs frorm FOR sentence) i want to edit. It always picks the latest.

Please help me!

ps: dont be afraid to ask if something wasnt clear.

pps: thanks in advance :wink:


#8

still need help :S


#9

please tell me exactly what are you trying to do so i can write a code for you.
Will the table kontrakt have more then one entry ?

P.S. Sorry i didnt come online, i had something to do :slight_smile:


#10

The table “kontrakt” has two entries; an entry called “tittel” and another called “tekst” (title and text in my language)

I’m pulling up DB entries with this sentence:
[php]

for ($r=1;$r<=$numRows;$r++)
{
$row=mysql_fetch_array($sqlResult);
print("

<input type=“text” name=“row”."$r" id=“row”."$r" value="$row[0]" readonly=“true”/> <input type=“text” name=“row2”."$r" id=“row2”."$r" value="$row[1]" readonly=“true”/> <input type=“submit” name=“rediger$r” id=“rediger$r” value=“Rediger” /> $row1=$row[0]; $row2=$row[1]; <input type=“hidden” name=“r” id=“r” value="$r"/> ;");
}[/php]

And i’d like to be able to edit those, problem is that whenever i press my little “edit button” it always pulls up the last entry and not the one im actually trying to edit (unless im trying to edit the last one of course).

I cannot for the life of me figure out what im doing wrong :confused:

Oh and thanks, andrutu991, for taking the time to help a newbie :slight_smile:


#11

your doing nothing wrong you just need a additional row to your table kontrakt called id this should be set auto_increment and primary_key, so every entry you add to your db will become one id, first entry will become id #1, second #2 and so on…

you have to select your entry out from db ordered by id Descending or ascending so you can get the newest or the oldest one first.

in order to get your table you will have to get to his id first, then you can select the table by id.

[php]<?php
// insert data to mysql //
// you will need a diferent row called id(auto_increment, primary key) //

$query = mysql_query(“INSERT INTO kontrakt(id,title,text) VALUES (’ ‘,’$title’,’$text’)”) or die(mysql_error());
if(@$query){ echo “Data added to database”;} // this is how you should add entry to your db

?>

<?php // get data from mysql // $query = mysql_query("SELECT * FROM `kontrakt` ORDER BY `id` DESC") or die(mysql_error()); // this will select all entry ordered by id descending or you can use SELECT * FROM `kontrakt` WHERE `id` = '$id' so you get one specific row while($data = mysql_fetch_array($query)){ $id = $data['id']; $title = $data['title']; $text = $data['text']; echo " Title: Text:$text"; } ?> <?php // edit data // $id = $_POST['id']; $title = $_POST['title']; $text = $_POST['text']; $query = mysql_query("UPDATE `kontrakt` SET `title`='$title' AND `text`='$text' WHERE `id`='$id' ") or die(mysql_error()); // this will update the entry which have the id $id if(@$query){ echo "Entry succesfully updated"; } ?>[/php]

I havent tested this but if you adjust to your needs it should work.


#12

Thanks, a few things i dont quite understand, though:

in [php]if(@$query)[/php] what does the [php]@ [/php] do?

Also, what is the difference between echo and print?

My teachers told me to use print.

(btw i dont know any object oriented programming, just good ol fashin php)


#13

The at-sign ( @ ) means SUPPRESS ERRORS. So, for istance, $xyz=@mail(blah…) if the mail function does not work correctly, the errors are suppressed and the $xyz is just set to null. So, if(@mail(blah)) would be null if it fails…

Print and Echo are basically the same thing…


#14

Ah…

You, sir, have taught me a valuable lesson :smiley:


#15

Glad I could help… Just don’t forget to remove the “@” if you need to print the error messages!

Good luck…


#16

Ok, im having some issues with this particular part of the code:
[php]$query = mysql_query(“SELECT * FROM kontrakt ORDER BY id DESC”) or die(mysql_error());
while($data = mysql_fetch_array($query)){
$id = $data[‘id’];
$title = $data[‘title’];
$text = $data[‘text’];
echo "

Title:

Text:$text"
;}[/php]

The problem is that i cant pick which of the entries i want to change.

I mean, this particular part pulls up several DB entries, which is fine, but i cant pick a particular one to edit.

If it generates 3 entries, how do i pick the 2nd entry?

The editing and all else is sound, i just need help finding out how to pick one to send to another page for editing.


#17

Also, i thought mysql_fetch_array only works with numeric or assoiative (or w.e) arrays.

I’m only bringing this up, because im trying out your array way and it’s not working for some reason. I only get blanks


#18
If it generates 3 entries, how do i pick the 2nd entry?
Do you mean that you wish to only have one item listed or that you want to do something special to the entry number 2?

If you want to have only the one entry listed, you would have to change your MySQL query to only pick out the one you want. Such and “WHERE id=‘2’” or something similar.

If you want to do something special for number just number 2 but see all the rest, how do you tell that one is special? You would put that condition into an IF clause around your display and alter it if it is the one you pick.
if(id=‘2’){
//do special stuff…
}else{
//do the other as normal…
}

So, you need to let us know what you are trying to do…


#19

Nevermind, i solved it in an overly complicated way :stuck_out_tongue:

And yes, i wanted to be able to pick any entry between 1-10, and NOT just the latest


#20

Nevermind doesn’t help others who are trying to solve it too! Please explain your solution or post sample code.
We helped you, please help others. Pay it forward…

Also, for general info on your comment:

Also, i thought mysql_fetch_array only works with numeric or assoiative (or w.e) arrays.
Actually, mysql_fetch_array will fetch anything in a row of a database record. This can be a picture, numeric data, an array of array or just about anything that PHP can place in a variable or arrays. When you execute a query, the server returns the "recordset" of the entire data your requested in the query. Then, when you "fetch" the data, usually you return one "row" of data in the table of the "recordset". This "row" is a list of fields which contain whatever data you placed in it. (Any possible PHP data!) Hope that explains it further. Glad you solved your project...