Lembre-se de que você já deve ter criado o banco e a conexão.
Para se incluir registros no banco de dados através do Dreamweaver, primeiramente, crie um formulário. Esse formulário deve ter os campos que serão armazenados no BD.
para facilitar, segue o código-fonte do arquivo que conterá o formulário.
arquivo: gravar.php
Gravando Dados
Uma pergunta: Onde está o campo para armazenarmos a ID?
Resposta: O Campo ID é autoNumeração, então o próprio MySQL se encarrega de preencher.
Após criar o campo, procure a guia "Server Behaviors", clique no "+" e escolha a opção "Insert Record", conforme a tela abaixo:
Em seguida, outra janela surgira, onde você irá simplesmente informar ao Dreamweaver, onde cada campo do formulário será armazenado com relação a tabela. Como estamos iniciando, sugiro que os mesmos nomes dos campos da tabela sejam os do formulário.
Se tudo der certo, o Dreamweaver inseriu um código PHP em sua página. Segue abaixo:
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF" target="_blank">;
if (isset($_SERVER['QUERY_STRING" target="_blank">)) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING" target="_blank">);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO agenda (nome, telefone, celular, e-mail) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['nome" target="_blank">, "text"),
GetSQLValueString($_POST['telefone" target="_blank">, "text"),
GetSQLValueString($_POST['celular" target="_blank">, "text"),
GetSQLValueString($_POST['e-mail" target="_blank">, "text"));
mysql_select_db($database_myconn, $myconn);
$Result1 = mysql_query($insertSQL, $myconn) or die(mysql_error());
$insertGoTo = "sucesso.php";
if (isset($_SERVER['QUERY_STRING" target="_blank">)) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING" target="_blank">;
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
Já está funcionando! Agora apenas crie um arquivo chamado "sucesso.php" com uma mensagem e um link de volta para o arquivo "gravar.php".
Você poderá conferir os registros gravados no banco de dados através do phpmyadmin.
No Próximo artigo da série, iremos aprender a retornar as informações gravadas na tela e na impressora!!!
Forte abraço, até a próxima.Dúvidas? Deixe seu recado que na medida do possível serão respondidos.
Recomendações do editor:
Assine a nossa newsletter