tandrli Posted September 26, 2014 Share Posted September 26, 2014 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 query table users so i can find out which prefix correspond with that user 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); ?> Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 26, 2014 Share Posted September 26, 2014 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); Quote Link to comment Share on other sites More sharing options...
Barand Posted September 27, 2014 Share Posted September 27, 2014 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"; } Quote Link to comment Share on other sites More sharing options...
tandrli Posted September 28, 2014 Author Share Posted September 28, 2014 (edited) 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 September 28, 2014 by tandrli Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.