my problem seems simple but I do not know much about PHP.
I have two tables:
Table 1: ** certificates ** / Table2: ** keys **
They don't have the same order or amount of columns. Table2 has predeterminated values while table1 is clean, receiving data inserted by the user.
The user uses a form to insert data into table1. In this form, there's a select populated with data from table2.
The select options are keys, like those:
1112, 1113, 1114, etc.
What I need is when the user insert one of the keys in table1, the status of the key in table2 is updated from 0 to 1. (so the key will not be shown in the select).
Here's the code that inserts the data into table1
<?php
if ($registro != "") {
include("conexao-certificado.php");
$conexao = @mysql_connect($host, $user, $pass)
or die("Cadastro");
$query = "INSERT INTO certificados VALUES ('$id','$registro','$aluno','$professor','$escola','$publicado_por','$registrado_por','$ano','$curso','$cpf','$grade', now() );";
mysql_query($query, $conexao);
$url = basename($_SERVER['PHP_SELF']);
echo "<script>window.location='$url'</script>";
} else {
}
?>
And here's the code that populates the select:
<?php
include("conexao-certificado.php");
$conexao = @mysql_connect($host, $user, $pass);
if (!$conexao)
die("Nao foi possivel conectar no servidor MySQL. Erro: " . mysql_error());
mysql_select_db($db) or die("Nao foi possivel usar o banco de dados. Erro: " .
mysql_error());
$palavra = str_replace(" ", "%", $HTTP_POST_VARS[palavra]);
$qr = "SELECT idchave FROM chaves WHERE (publicado_por LIKE '%" . $_SESSION['nome'] . "%" . $_SESSION['sobrenome'] . "%' AND status LIKE '0' ) ORDER BY idchave ASC;";
$sql = mysql_query($qr);
?>
<select name="registro" id="registro">
<?php
echo '<option value="">Selecione a chave</option>';
while ($r = mysql_fetch_array($sql)) {
echo '<option value="' . $r['idchave'] . '">' . $r['idchave'] . '</option>';
}
?>
</select>
I tried puting this after the INSERT, but it only works if i specifie the key like "idchave=1112".
$atualiza = "UPDATE chaves SET status=1 WHERE idchave=$registro;";
mysql_select_db($db);
mysql_query($query,$conexao);
mysql_query($atualiza,$conexao);
I found a solution. So here's what I did. First a created a column in table1 with status always being 1 while on table2 the status are always 0.
After that I just added a INNER JOIN in my code.
Here's the code:
$atualiza = "UPDATE chaves c1
INNER JOIN certificados c2 ON c1.registro = c2.registro
SET c1.status = c2.status;";
mysql_query($atualiza,$conexao);
Thank you for all the help.