-4

When i run my code, the navegator show me that:

Notice: Use of undefined constant datos1 - assumed 'datos1' in C:\xampp\htdocs\formulario\conexion1.php on line 8

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\formulario\conexion1.php on line 8 No se pudo conectar a la base de datos Notice: Undefined index: Teachers_Name in C:\xampp\htdocs\formulario\conexion1.php on line 13

Notice: Undefined index: School_Name in C:\xampp\htdocs\formulario\conexion1.php on line 14

Notice: Undefined variable: Implementation_Quality in C:\xampp\htdocs\formulario\conexion1.php on line 15

Notice: Undefined variable: Implementation_Quality in C:\xampp\htdocs\formulario\conexion1.php on line 17

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\formulario\conexion1.php:19 Stack trace: #0 {main} thrown in C:\xampp\htdocs\formulario\conexion1.php on line 19

What i do?

if(!$conexion){
echo "Conexión no exitosa";
} else {

$base= mysqli_select_db(datos1);
    if(!$base){
        echo "No se pudo conectar a la base de datos";
    }
 }
$Teachers_Name=$_POST['Teachers_Name'];
$School_Name=$_POST['School_Name'];
$Implementation_Quality['Implementation_Quality'];

$sql= "INSERT INTO datos_1 VALUES('$Teachers_Name', 'School_Name', 
'$Implementation_Quality')";

$ejecutar = mysql_query($sql);

if(!$ejecutar){
echo "Hubo algun error";
} else {
 echo "Datos guardados correctamente<br><a href='index.html'>Volver</a>";
}
?>
Kisaragi
  • 2,114
  • 3
  • 16
  • 27
Marinovsky
  • 39
  • 2
  • 8
  • 2
    You're mixing up `mysqli_*` and `mysql_*`, which are different libraries. MYSQL_* is deprecated and no longer available at PHP 7 or larger. – KhorneHoly Nov 15 '17 at 16:32
  • in here you must set name filed of your table..(You must specify your name field from your table)) $sql= "INSERT INTO datos_1 (filde1, filde2, filde3) VALUES('$Teachers_Name', 'School_Name', '$Implementation_Quality')"; – pedram shabani Nov 15 '17 at 16:35
  • 3
    You've posted a variety of different error messages, as well as code with a variety of different problems. The help you're looking for is in the form of introductory tutorials on PHP and MySQL. Google is a good place to find those. – David Nov 15 '17 at 16:35
  • @pedramshabani: That's not required if the supplied values successfully map to the table structure. (Though it's still a good idea to explicitly specify column names.) – David Nov 15 '17 at 16:36
  • Ok.Thank you David.i don't know it before...... – pedram shabani Nov 15 '17 at 16:39
  • 2
    @Marinowsky: Welcome to the StackOverflow community! As a programmer, it is imperative that you develop the skills needed to debug code that you've written. I strongly recommend you do a read of this https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and the information on the "how to ask" page. StackOverflow is a question answer site. And "Here's my code, it's not working, please help me!" isn't really a *question*. I've provided some tips in an answer, I hope you find it helpful. – spencer7593 Nov 15 '17 at 17:11

1 Answers1

1

Replace this:

$base= mysqli_select_db(datos1);

With this:

$base= mysqli_select_db($conexion,'datos1');

replace this:

$sql= "INSERT INTO datos_1 VALUES('$Teachers_Name', 'School_Name', '$Implementation_Quality')";

With this:

$sql= 'INSERT INTO datos_1 (col1, col2, col3) VALUES ( ?, ?, ? )';

Replace col1,col2 and col3 with the names of columns in the datos_1 table.
e.g. ... datos_1 (teacher_name, school_name, implementation_quality) VALUES ...

Note that this is a static string literal. There's no variable substitution. The question marks are bind placeholders which we will reference later.


Replace this:

$ejecutar = mysql_query($sql);

With this:

$sth = mysqli_prepare($conexion,$sql);
mysqli_stmt_bind_param($sth,'sss', $Teachers_Name, $School_Name, $Implementation_Quality);
$ejecutar = mysqli_stmt_execute($sth);

Don't mix mysql_ functions with mysqli_ functions. That won't work. (We shouldn't be using any mysql_ functions in new development; the mysql_ interface functions have been deprecated a long time ago, and are finally removed in newest versions of PHP.


Do NOT incorporate potentially unsafe values in SQL text. Use prepared statements with bind placeholders. (Or less optimally, properly escape any values that are incorporated into the text.)

Recommended:

Little Bobby Tables - Exploits of a Mom https://xkcd.com/327/

OWASP SQL Injection https://www.owasp.org/index.php/SQL_Injection

spencer7593
  • 103,596
  • 14
  • 107
  • 133
  • you missed something here – Funk Forty Niner Nov 15 '17 at 16:39
  • *"... more"* - Are you still editing? *lol!* – Funk Forty Niner Nov 15 '17 at 16:39
  • @Fred -ii- .posting before the question was closed. ... still editing – spencer7593 Nov 15 '17 at 16:41
  • wasn't my downvote you just got here – Funk Forty Niner Nov 15 '17 at 16:44
  • @Fred-ii- this answer probably deserves more downvotes,.. i was attempting to give some direction to a student who appears to be attempting to learn. It's not a great question. Probably even a horrible question. But I hope OP can glean some help from my answer. (I'm not gonna give a copy/paste here's working code. I'm just pointing out some of the lines that look wrong, and supplying some suggested replacements to fix the most apparent problems. – spencer7593 Nov 15 '17 at 16:57
  • I feel you've taught them well :-) – Funk Forty Niner Nov 15 '17 at 17:01
  • Personally, I'd use PDO rather than mysqli. With mysqli, i'd prefer to use the OO style rather than the procedural style. But I DEFINITELY would not give example code with a pattern that is vulnerable to SQL Injection. – spencer7593 Nov 15 '17 at 17:05
  • As you had said before. Yes, im beginning to learn PHP, and SQL. I only have 16 years old, and I know a little bit of Javascript, and C#. Please, bear with me. – Marinovsky Nov 17 '17 at 14:08
  • @Marinowsky: I applaud your efforts. And as I said in my comment, Welcome to the StackOverflow community. (Please understand that StackOverflow is intended to be a Question/Answer site, and the community actively works to keep it QA. Duplicate questions, questions that don't show research or don't present a clear problem statement are frequently closed. So please bear with us.) I strongly recommend a review of SQL Injection vulnerabilities, and *please* learn and follow patterns that mitigate it. The interwebs are flooded with examples of vulnerable code. Eschew those examples. – spencer7593 Nov 17 '17 at 16:23
  • Ok, i will do that. Thanks for explain me, how to solve my problem in my code. – Marinovsky Nov 17 '17 at 16:37