Jump to content

POST variable, do query and insert into table same php


Recommended Posts

hi ..

hit little snag Ive got html <form name="formx" method="POST" action="connect.php" class='ajaxform'> and about 4 varibles in that form .. now how to properly

  1. query table users so i can find out which prefix correspond with that user
  2. Insert into table "upis" in this case that prefix with 3 variables..

everything in single,.. in this case connect.php file

I know how to do QUERY and INSERT INTO but i don't know how to connect with 2 different tbl or db and do the query and input of data.. every time I put 2 of them together .. never works out..

this is example.. i dont know is this right way to do it ..

 

so any guidence ... thnx

<?php
$rrr = $_POST['user'];
$conn = mysql_connect("localhost", "root", "111");
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT prskola FROM users WHERE user_name = "' . $rrr. '"';
mysql_select_db('login');
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$value1 = $row['prskola'];
echo "$value1"; // it works got correct data
}
mysql_close($conn);
?>
<?php
$skola=mysql_connect("localhost" , "root" , "111") or die (mysql_error());
mysql_select_db ("skola", $skola);
error_reporting (E_ALL ^ E_NOTICE);
$value2 = $_POST['data1'];
$value3 = $_POST['data2'];
$value4 = $_POST['data3'];
$sql = "INSERT INTO `upis`(`col1`, `col2`, `col3`, `col4`)
VALUES ('$value1', '$value2', '$value3', '$value4')";
mysql_query($sql, $skola);
?>

Are you getting any errors? If so, what are they?

 

Also, it's worth mentioning that errors could be hidden on your server. To enable all errors and warnings, you can add the following to the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

You do not need to connect twice to the server, it wastes time.Most of the time used in the execution of the script will be the database connect time. You can access different databases by prefixing the the table name with the database name (even in the same query)

 

eg login.users and skola.upis.

 

You could also accomplish your task with a single insert query

INSERT INTO skola.upis(`col1`, `col2`, `col3`, `col4`)
        SELECT prskola, '$value2', '$value3', '$value4'
        FROM login.users WHERE user_name = '$rrr' 

However, rather than inserting raw user-provided data directly into a query you should use a prepared query. So you would have

$conn = new mysqli("localhost", "root", "111", "login");
if ($conn->connect_error) {
    die('Connect Error (' . $conn->connect_errno . ') '
            . $conn->connect_error);
}

$rrr = $_POST['user'];
$value2 = $_POST['data1'];
$value3 = $_POST['data2'];
$value4 = $_POST['data3'];

$sql = "INSERT INTO skola.upis(`col1`, `col2`, `col3`, `col4`)
        SELECT prskola, ?, ?, ?
        FROM login.users WHERE user_name = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('ssss', $value2, $value3, $value4, $rrr);
$stmt->execute();
if ($stmt->affected_rows==0) {
    echo "Could not get data";
}

Sorry .. Wasn't home so I could not respond and test questions thanks for responding

@Barand .. that is some new age shit (pardon my french) which I'm not even on the doorstep to comprehend ... well in  do time...

so one more question if I may .. Because when I run this PHP alone .. it works ok as it should .. but when I include it into index.php gives me DB error ..
I ran it 2 days ago and it was working fine but now I only get a printout .. with no data

 

 

config.php

<?php
$query=mysql_connect("localhost","root","111");
mysql_select_db("skola",$query);
?>

list.php (config.php has correct connection data as I said it runs OK when I run list.php solo)

<?php
include('config.php');
$query1=mysql_query("select id, `SUcenika`, `Prezime`, `Ime`, `DatumRodjenja`, `SPol`, `MjestoRodjenja`, `DrzavaRodjenja`, `Prebivaliste`, `SOpcina`, `SIspit`, `SPrviPredmet`, `SDrugiPredmet`, `SJezikPolaganja`, `SPismoPolaganja`, `Odjeljenje` from upis");
echo "<table><tr><td>Skola</td><td>Prezime</td><td>Ime</td><td>DatumRodenja</td><td>Spol</td><td>MjestoRodenja</td><td>Drzava Rodenja</td><td>Prebivaliste</td><td>Opstina</td><td>Ispit</td><td>Prvi predmet</td><td>Drugi predmet</td><td>Jezik polaganja</td><td>Pismo polaganja</td><td>Odjeljenje</td><td></td><td></td>";
if($query1 === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
while($query2=mysql_fetch_array($query1))
{
echo " /<tr><td>".$query2['SUcenika']."</td>";
echo " /<td>".$query2['Prezime']."</td>";
echo " /<td>".$query2['Ime']."</td>";
echo " /<td>".$query2['DatumRodjenja']."</td>";
echo " /<td>".$query2['SPol']."</td>";
echo " /<td>".$query2['MjestoRodjenja']."</td>";
echo " /<td>".$query2['DrzavaRodjenja']."</td>";
echo " /<td>".$query2['Prebivaliste']."</td>";
echo " /<td>".$query2['SOpcina']."</td>";
echo " /<td>".$query2['SIspit']."</td>";
echo " /<td>".$query2['SPrviPredmet']."</td>";
echo " /<td>".$query2['SDrugiPredmet']."</td>";
echo " /<td>".$query2['SJezikPolaganja']."</td>";
echo " /<td>".$query2['SPismoPolaganja']."</td>";
echo " /<td>".$query2['Odjeljenje']."</td>";
echo " /<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
?>

tabela.html is HTML who has included php file list.php

if ($login->isUserLoggedIn() == true) {
    include("tabela.html");
   } else {
   //not loged in
}
?>

printout

No database selected
Skola	Prezime	Ime	DatumRodenja	Spol	MjestoRodenja	Drzava Rodenja	Prebivaliste	Opstina	Ispit	Prvi predmet	Drugi predmet	Jezik polaganja	Pismo polaganja	Odjeljenje		

also here is dump of db

skola.zip

Edited by tandrli
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.