Save php Mysql data in a single table

Asked

Viewed 268 times

0

I have a small question about saving a form in mysql that is. I have a form that is divided into 2 and I want it to be stored in the same table.

When I save only make save the second part of mysql it erases me the first.

Example Formulary1: Name, function Insert into the comic book:

$sqlinsert = "Insert into tb_trabalhador (Nome, funcao) Values ('".$Nome."','".$Funcao."')";

mysql_query($sqlinsert) or die(mysql_error());
if ($sqlinsert)

Formulario2: Letter, date of birth Insert into the comic book:

$sqlinsert = "Insert into tb_trabalhador (Cartao, Data) Values ('".$Cartao."','".$Data."')";

 mysql_query($sqlinsert) or die(mysql_error());
 if ($sqlinsert)
  • 1

    It seems you are recreating the variable and overwriting the query, post more of your code so we can understand.

  • but it’s two different php pages. I don’t think it interferes

  • 1

    You can interfere yes. We need to see more code to help.

  • The two are exactly the same in the Insert part. Only changes are the fields.

  • Changing only sqlinsert2 , sqlinsert3... can now help work?

1 answer

4

The problem is that it seems to me that you want to join the data in one record only, but are creating two.

After using each of your codes, you will have a table like this:

Cartao | Data       | Nome   | Funçao
-------+------------+--------+--------
2136   | 31/05/2014 |        |
-------+------------+--------+--------
       |            | Holmer | Estivador

If you want to add data to a table, you’ll need something like this:

$cartao = "2136" // esse é só um exemplo, pode ser ID, aí vai depender do seu caso.
$sqlupdate = "UPDATE tb_trabalhador". // ATUALIZE tb_trabalhador
    "SET Nome=$nome, Funcao=$Funcao". // mudando o nome pra $Nome e função pra $Funcao
    "WHERE Cartao=$Cartao";           // na linha em que o cartão for $Cartao

Note: this is just an example. As you may have been warned in some of your previous questions, you should be using mysqli_ to use bindParameter, instead of creating the string with parameters, but this is already another problem.

  • I will have no problem using the update to add mysql data?

  • Probably will, but not for the same reason ;) . Joking aside, the difference is as follows: INSERT is to add a new row in the table, with the data you provide. UPDATE only changes an existing line by changing cell data. If you put 3 information with INSERT, you will be creating 3 rows (different records) with all fields in the table, even if empty. On the other hand, with UPDATE will never be created a new line, it only serves to update the information of an existing line.

  • Let’s make a comparison so you understand: the INSERT is you take a new form from the drawer, with several empty fields, put the name of the client in it, and age, and store in the folder "clients". UPDATE is you take some of the forms that are already in the "clients" folder and add something, (or pass corrective and change something). Applying in your case: in your code, you took two brand-new customer tokens from the drawer, one of them placed the Card and the Date. In the other, put Name and Function, and now has two client chips in the same folder, both missing piece.

  • a doubt. if I am entering the data do not know what data I am putting. How can I update by some field or by id?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.